# encoding: ascii
# api: powershell
# title: validate an IP address
# description: Uses [system.net.IPAddress]::parse, not [system.net.IPAddress]::tryparse.
# version: 127.1
# author: mow01
# license: CC0
# x-poshcode-id: 5781
# x-derived-from-id: 5782
# x-archived: 2016-03-17T03:21:50
# x-published: 2016-03-12T06:46:00
#
# The result is then tested against the original parameter.
#
# validate given IP address as an IPAdress (given string input)
PARAM($ip=$(read-host "Enter any IP Address"))
try
{
#parse fills missing octets with zeros in the order of 3rd, 2nd, 1st
#("127.1" becomes "127.0.0.1") ("192.168.1" becomes "214.0.0.1") ("1" becomes "0.0.0.1")
#test if converted IP matches original IP. If invalid, throws MethodInvocationException
return [system.net.IPAddress]::parse("$ipAddress").IPAddressToString -eq "$ipAddress"
}
catch{return $false}