11-29-2008
Downloads: 487
File size: 505 B
Views: 3,884
Embed
 |
Validate IP Address string (alternative) |
-
-
-
-
-
- $strings = "10.1.1.100", "10.1", "fooey"
-
- foreach( $string in $strings){
- "Testing: `"{0}`"" -f $string
- $address = [System.Net.IPAddress]::tryparse($string,[ref] $ip1)
- if ($address ){
- "`"{0}`" `tis a valid IP address" -f [System.Net.IPAddress]::parse($string)
- }
- else {
- "`"{0}`" `tis NOT a valid IP address" -f $string
- }
-
- }
This script demonstrates an alternative way to validate an string as being an IP address using [system.net.ipaddress]::try(). This method returns a true/false value to say if the string is a valid IP address, and it returns a [ref] value of the IPAddress object. Compare this with: http://powershell.com/cs/media/13/default.aspx