Last active
May 12, 2020 14:24
-
-
Save gsscoder/87aaec234fb1e697db8df185b9ef51f7 to your computer and use it in GitHub Desktop.
PowerShell validation helper functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Url([Parameter(ValueFromPipeline)] [string] $url) { | |
$url -cmatch '^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$' | |
} | |
function Test-Guid([Parameter(ValueFromPipeline)] [string] $guid) { | |
try { [System.Guid]::Parse($guid) | Out-Null; $true } catch { $false } | |
} | |
function Test-IPAddress([Parameter(ValueFromPipeline)] [string] $ip, [switch] $cidr) { | |
if ($cidr) { $ip -match '^(?:\d{1,3}\.){3}\d{1,3}/\d\d$' } | |
else { try { [System.Net.IPAddress]::Parse($ip) | Out-Null; $true } catch { $false } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment