{"id":39,"date":"2019-04-23T21:48:41","date_gmt":"2019-04-23T19:48:41","guid":{"rendered":"http:\/\/sys-code-alpha.com\/?p=39"},"modified":"2019-04-23T22:09:49","modified_gmt":"2019-04-23T20:09:49","slug":"quick-way-to-check-ip-address-in-powershell","status":"publish","type":"post","link":"https:\/\/sys-code-alpha.com\/?p=39","title":{"rendered":"Quick way to filter out wrong IP address in PowerShell"},"content":{"rendered":"\n<p>There are multiple ways to validate IP address using PowerShell. One of them is to use regex. <\/p>\n\n\n\n<p>The regex itself is pretty much far from being lightweight. So we end up with this: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$<\/code><\/pre>\n\n\n\n<p>The safest way to get it in the console is either copy it from a separate file or type regex for one octet after another and add them together:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-1-1024x163.png\" alt=\"\" class=\"wp-image-41\" width=\"826\" height=\"131\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-1-1024x163.png 1024w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-1-300x48.png 300w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-1-768x122.png 768w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-1.png 1376w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/figure>\n\n\n\n<p>Although it works well, it is not the way that fits my preference.<\/p>\n\n\n\n<p>There is another one. We can use ipaddress class and parse method. It looks as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-2.png\" alt=\"\" class=\"wp-image-42\" width=\"707\" height=\"329\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-2.png 960w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-2-300x140.png 300w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-2-768x358.png 768w\" sizes=\"(max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p>In case parsing is successful it returns a nice IP address object. There is another problem with this approach however. If we do something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"568\" height=\"228\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-3.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-3.png 568w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-3-300x120.png 300w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-3-565x228.png 565w\" sizes=\"(max-width: 568px) 100vw, 568px\" \/><\/figure>\n\n\n\n<p>Or:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"447\" height=\"229\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-4.png\" alt=\"\" class=\"wp-image-44\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-4.png 447w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-4-300x154.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/figure>\n\n\n\n<p>We get a parsed IP address anyway.<\/p>\n\n\n\n<p>Although it might appear for some as a reasonable way to deal with parsing strings I would personally prefer to get an error and parse only when I have all four octets defined.<\/p>\n\n\n\n<p>Looking for a quick way to handle this differently I ended up with a quick and funny filter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Filter Test-IPAddress{ \n    PROCESS{ \n        IF(($_ -split '\\.' ).count -eq 4){$IP = $_}\n        IF($IP -eq  (( $IP | ForEach-Object{$_ -split \"\\.\"} | ForEach-Object{  IF($_ -in @(0..255)){$_}} ) -join '.' )){$IP}  \n    }\n} <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-6-1024x96.png\" alt=\"\" class=\"wp-image-46\" width=\"821\" height=\"76\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-6-1024x96.png 1024w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-6-300x28.png 300w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-6-768x72.png 768w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-6.png 1404w\" sizes=\"(max-width: 821px) 100vw, 821px\" \/><\/figure>\n\n\n\n<p>Now I can pipe an array of strings and filter out the wrong IP addresses.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"757\" height=\"106\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-7.png\" alt=\"\" class=\"wp-image-47\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-7.png 757w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/image-7-300x42.png 300w\" sizes=\"(max-width: 757px) 100vw, 757px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple ways to validate IP address using PowerShell. One of them is to use regex. The regex itself is pretty much far from being lightweight. So we end up with this: The safest way to get it in the console is either copy it from a separate file or type regex for one [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/39"}],"collection":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":6,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":54,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/54"}],"wp:attachment":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}