Validating IP-Addresses

To check for a valid IP-address, use the .NET Framework type System.Net.IPAddress and test whether the data can be converted into this format:

function isIPAddress($object) {
($object -as [System.Net.IPAddress]).IPAddressToString -eq $object -and $object -ne $null
}

isIPAddress "10"
isIPAddress "127.0.0.1"
IsIPAddress "hello"
isIPAddress $null

Posted Jan 30 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.