PowerShell stuff: Validating an IP address - Revisited

Every once in a while I write these huge blocks of code to do something really cool only to find out .NET has already got that built in.  Take my post from yonder days about validating an IP address…

http://www.orcsweb.com/blog/james/fun-with-powershell-testing-whether-an-ipv4-address-has-a-valid-structure/

It turns out .NET has already got that built in and all those lines of code can be condensed into this:


$ipAddress = “192.168.1.100”
$ipObj = [System.Net.IPAddress]::parse($ipAddress)
$isValidIP = [System.Net.IPAddress]::tryparse([string]$ipAddress, [ref]$ipObj)
if ($isValidIP) {
   Write-host “$ipAddress is valid”
} else {
   Write-host “$ipAddress is not valid”
}

For those who like the MSDN references you can take a gander at them here:
 
In the words of the great comedian Eric Idle, “That was easy!”  Mental note, search MSDN for .NET libraries before diving off into code that probably already exists.
 
#James Kehr
Get-Member $OrcsWeb | ?{$_.title -eq "System Administrator"`
-and $_.certification -contains 'MCITP:SA 2008, MCSE 2000, MCDST, Network+, A+'}

New-Variable -name company -value 'ORCS Web, Inc.' -description www.orcsweb.com | 1.888.313.9421’
blog comments powered by Disqus