40 lines
1.1 KiB
PowerShell
40 lines
1.1 KiB
PowerShell
function Test-IsStringIPAddress {
|
|
<#
|
|
.SYNOPSIS
|
|
Determines if a string is an IP Address
|
|
|
|
.DESCRIPTION
|
|
Compares a string against the regular expression "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
|
to determine if it is a valid IP address
|
|
|
|
.PARAMETER stringToCheck
|
|
[string] A string to check if it is an IP
|
|
|
|
.INPUTS
|
|
Accepts a string from the pipeline
|
|
|
|
.OUTPUTS
|
|
A boolean indicating if the string is a valid IP address
|
|
|
|
.EXAMPLE
|
|
Test-IsStringIPAddress "127.0.0.1"
|
|
#>
|
|
[CmdletBinding()]
|
|
[OutputType([System.Boolean])]
|
|
param(
|
|
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
|
|
[string]$stringToCheck
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
[Regex]$ipMatchRegex = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
|
|
|
if ($stringToCheck -match $ipMatchRegex) {
|
|
Write-Verbose "$logLead : String $stringToCheck Matched IP Regex"
|
|
return $true
|
|
}
|
|
|
|
Write-Verbose "$logLead : String $stringToCheck Does Not Match IP Address Regex"
|
|
return $false
|
|
}
|