Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active May 12, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/87aaec234fb1e697db8df185b9ef51f7 to your computer and use it in GitHub Desktop.
Save gsscoder/87aaec234fb1e697db8df185b9ef51f7 to your computer and use it in GitHub Desktop.
PowerShell validation helper functions
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