PoshCode Archive  Artifact [08ac76a9c2]

Artifact 08ac76a9c213633126325948c0cf79d6636600838f7112bbfcd39733accc8e06:

  • File validate-an-IP-address.ps1 — part of check-in [4db576c824] at 2018-06-10 12:56:35 on branch trunk — REALLY validates given IP address and returns True/False. The original script didn’t allow ZEROS in the Ip address (eg: 127.0.0.1 returned $false) (user: ucthakur size: 878)

# encoding: ascii
# api: powershell
# title: validate an IP address
# description: REALLY validates given IP address and returns True/False.  The original script didn’t allow ZEROS in the Ip address (eg: 127.0.0.1 returned $false)
# version: 0.1
# author: ucthakur
# license: CC0
# x-poshcode-id: 1171
# x-derived-from-id: 1172
# x-archived: 2015-02-28T03:12:52
# x-published: 2010-06-24T08:48:00
#
#
# validate given IP address as an IPAdress (given string input)
PARAM($IP=$(read-host "Enter any IP Address"))

## YOU could do this, but ...
# $IP -match "(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})" -and -not ([int[]]$matches[1..4] -gt 255)

## you shouldn't parse things yourself when it's in the framework. You might make a mistake ;-)
trap { return $false }
[IPAddress]$IP  # Just cast it to an IPAddress ... if it's valid, it will work.
return $true