PowerShell IP Address Type Accelerator Not 100% Accurate

Testing whether or not an IP Address is valid with PowerShell is a fairly simple process with the [ipaddress] type accelerator if your goal is to validate input for a script or function as shown in the following code example:

 1function Test-ValidInput {
 2
 3    [CmdletBinding()]
 4    param (
 5        [ipaddress]$IpAddress
 6    )
 7
 8    Write-Output $true
 9
10}

When a valid IP address is specified, the function continues and when an invalid IP address is specified, the function terminates immediately and returns an error message:

1Test-ValidInput -IpAddress 192.168.0.1
2Test-ValidInput -IpAddress 192.168.0.256

testip-1a.png

The IP Address type accelerator can also be used to validate IPv6 addresses:

1Test-ValidInput -IpAddress ::1
2Test-ValidInput -IpAddress ::1:1

testip-6a.png

As you can see in the previous sets of results, input validation of an IP address for a script or function in PowerShell is an almost no code solution although I've seen others write an enormous amount of code to accomplish this task, but is the IP address type accelerator in PowerShell accurate?

What I discovered is the IP address type accelerator in PowerShell isn't 100% accurate. Entering a number that's five 2's seems to be cast to a valid IP address so the type accelerator thinks it's valid (clearly it isn't):

1Test-ValidInput -IpAddress 22222
2[ipaddress]'22222'

testip-2a.png

One simple line of code can weed out those inaccurate results:

1'22222' -match [IPAddress]'22222'

testip-3a.png

ValidateScript along with a little code can be used to prevent this scenario from occurring when performing input validation for IP addresses:

 1function Test-ValidInput {
 2
 3    [CmdletBinding()]
 4    param (
 5        [ValidateScript({
 6            If ($_ -match [IPAddress]$_) {
 7                $true
 8            }
 9            else {
10                Throw "Cannot convert value `"$_`" to type `"System.Net.IPAddress`". Error: `"An invalid IP address was specified.`""
11            }
12        })]
13        [string]$IpAddress
14    )
15
16    Write-Output $true
17
18}

The IP address validation now works as expected as shown in the following example:

1Test-ValidInput -IpAddress 192.168.0.1
2Test-ValidInput -IpAddress 192.168.0.256
3Test-ValidInput -IpAddress 22222

testip-4a.png

Update:

I also discovered that the same five digit number is translated to a valid IP address when using the ping command from the command prompt which completely eliminates PowerShell:

1ping 22222

testip-7a.png

Maybe someone can explain why this occurs? See the comments to this blog article for the explanation.

The examples shown in this blog article were written and tested on Windows 10 Enterprise Edition running PowerShell version 5.1.14393.206.

Update #2:

There's been lots of discussion about this blog article on Twitter, one interesting response caught my eye and is also an IP address that you wouldn't think would be valid, but both the IP address accelerator in PowerShell and ping via the command prompt expand 10.1 as 10.0.0.1:

testip-9a.png

1[ipaddress]'10.1'

testip-10a.png

The code used in my Test-ValidInput function previously shown in this blog article weeds out those unexpected results as well:

1Test-ValidInput -IpAddress '10.1'

testip-11a.png

The results for integers like '22222' and IP addresses like '10.1' aren't necessarily wrong, but unexpected if you're trying to validate the input is a valid IP address specified in quad-dotted notation of four decimal integers.

µ