Skip to content

Instantly share code, notes, and snippets.

@mwjcomputing
Created July 24, 2014 13:05
Show Gist options
  • Select an option

  • Save mwjcomputing/81d4139d139e50b858e3 to your computer and use it in GitHub Desktop.

Select an option

Save mwjcomputing/81d4139d139e50b858e3 to your computer and use it in GitHub Desktop.
This function shows a cleanly written and well documented function.
function Test-IPv4Address {
[CmdletBinding()]
param(
[parameter(Mandatory=$true, ValueFromPipeline=$true, HelpMessage='Enter data to validate as IP Address.')]
[alias('IPAddress')]
[string]$IP
)
begin{}
process{
Write-Verbose -Message 'Checking to see if data entered is an IP address.'
if ($IP -as [IPAddress])
{
Write-Verbose -Message 'Data has been validated as a valid IP addresss.'
$true
} else {
Write-Verbose -Message 'Data has been validated as it is NOT a valid IP address.'
$false
}
Write-Verbose -Message 'Finished validating data.'
}
end{}
<#
.SYNOPSIS
Validates an IP address.
.DESCRIPTION
This function takes data in and validates if it is an IP Address.
.PARAMETER IP
Data to be validated as an IP Address
.INPUTS
[System.String]
.OUTPUTS
[System.Bool]
.EXAMPLE
Test-IPv4Address -IP 10.0.0.1
True
.EXAMPLE
Test-IPAddress -IP 260.150.3.1000
False
.LINK
http://www.mwjcomputing.com
.LINK
http://github.com/mwjcomputing
.NOTE
This code was modified from a post at http://www.powershellmagazine.com/2014/07/23/how-ive-started-loving-powershell by Fabien Dibot.
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment