A task that I have had to do several times over the years is determine if an IP Address string entered from a prompt or imported from a file is indeed valid. Even more, determining if that IP Address is in use once verifying that the IP Address is well formed.
I’ve created the following Powershell function – Test-IPAddressString – to handle both of these tasks.
Either provide a single or several IP Address candidates to this function and it will return a $true if it is valid or a $false if it is not. As an added bonus, if you enable the –FailIfInUse swtich,the function will attempt to ping a valid IP Address. If the address responds to a ping, $false is returned.
function Test-IPAddressString { <# .SYNOPSIS Tests to see if an IP address string is a valid address and can determine if IP Address is responding on network. .DESCRIPTION Implements the .Net.IPAddress classes to validate that the string provided can parse into an IP Address. If the parse is successful, the function returns boolean $true unless the -FailIfInUse switch is provided. If -FailIfInUse is specified, a $true is only returned if the IP Address does not respond to a ping three times. This function supports the -Verbose switch as well. .PARAMETER IPaddressString This is a string value representing the address that you want to test. This value can be passed either from pipeline or as a parameter. .PARAMETER FailIfInUse This is a switch parameter. Specifying this in the parameter list will cause the IP Address, if successfully parsed, to attempt to be pinged. A successful ping result will result in a $false value being returned if this switch is used. .EXAMPLE Test-IPAddressString -IPAddressString "192.168.1.1" Using full parameter name .EXAMPLE Test-IPAddressString "192.168.1.1" -FailIfInUse Using Positional parameters and specifiying to fail test if IP Address responds to ping. .EXAMPLE ("192.168.1.1", "192.123456", "IsThisAnIP") | Test-IPAddressString -Verbose Passing serveral potential IP Address strings to the function through the pipeline. FailIfInUse is ommitted, so no ping attempts will be made on valid IP Addresses. This function supports the verbose switch. Using this switch will provide you with several indicators of progress through the process. .NOTES Author: Kyle Neier Blog: http://sqldbamusings.blogspot.com Twitter: Kyle_Neier .LINK http://sqldbamusings.blogspot.com/2012/04/powershell-validating-ip-address.html #> param( [parameter( Mandatory=$true, Position=0, ValueFromPipeline= $true )] [string]$IPaddressString, [parameter( Mandatory=$false, Position=1, ValueFromPipeline= $false )] [switch]$FailIfInUse ) process { [System.Net.IPAddress]$IPAddressObject = $null if([System.Net.IPAddress]::tryparse($IPaddressString,[ref]$IPAddressObject) -and $IPaddressString -eq $IPAddressObject.tostring()) { Write-Verbose "$IPaddressString successfully parsed." if($FailIfInUse -eq $true) { $Pinger = new-object System.Net.NetworkInformation.Ping $p = 1 $p_max = 3 do { Write-Verbose "Attempting to ping $IPaddressString - Attempt $p of $p_max" $PingResult = $Pinger.Send("$IPaddressString") Write-Verbose "Connection Result: $($PingResult.Status)" $p++ Start-Sleep -Milliseconds 500 } until ($PingResult.Status -eq "Success" -or $p -gt $p_max) if($PingResult.Status -eq "Success") { Write-Verbose "The IP Address $IPAddressString parsed successfully but is responding to ping." Write-Output $false } else { Write-Verbose "The IP Address $IPAddressString parsed successfully and is not responding to ping." Write-Output $true } } else { Write-Verbose "The IP Address $IPAddressString parsed successfull - No ping attempt made." Write-Output $true } } else { Write-Verbose "The IP Address $IPAddressString could not be parsed." Write-Output $false } } }
About Kyle Neier
Husband of a magnificent woman, father of 5, SQL Server geek, IndyPASS Vice President and Food Guy, DBA automation zealot, amateur Powershell evangelist. Follow Me on Twitter
No comments:
Post a Comment