Wednesday 3 December 2008

Get-IPAddress.ps1

#Requires -Version 2.0
#
Get-IPAddress.ps1
#
Parses an address, returns a System.NET.IPAddress if OK
#
Thomas Lee - tfl@psp.co.uk

$strings = "10.1.1.100", "10.1", "fooey"

foreach( $string in $strings){
try {
$address = [System.Net.IPAddress]::parse($string)
"`"{0}`" `tis a valid IP address" -f $address.IPaddressToString
}
catch {
"`"{0}`" `tis NOT a valid IP address" -f $string
}
}

This script produces the following output:

PS C:\foo> . \GetIPAddress.ps1
"10.1.1.100" is a valid IP address
"10.0.0.1" is a valid IP address
"fooey" is NOT a valid IP address




No comments: