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:

function Test-ValidInput {

[CmdletBinding()]
param (
[ipaddress]$IpAddress
)

Write-Output $true

}

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:

Test-ValidInput -IpAddress 192.168.0.1
Test-ValidInput -IpAddress 192.168.0.256

testip-1a.png

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

Test-ValidInput -IpAddress ::1
Test-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):

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

testip-2a.png

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

'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:

function Test-ValidInput {

[CmdletBinding()]
param (
[ValidateScript({
If ($_ -match [IPAddress]$_) {
$true
}
else {
Throw "Cannot convert value `"$_`" to type `"System.Net.IPAddress`". Error: `"An invalid IP address was specified.`""
}
})]
[string]$IpAddress
)

Write-Output $true

}

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

Test-ValidInput -IpAddress 192.168.0.1
Test-ValidInput -IpAddress 192.168.0.256
Test-ValidInput -IpAddress 22222

testip-4a.png

Please share your thoughts, suggestions, and/or comments via a comment to this blog article.

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:

ping 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

[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:

Test-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.

ยต