PowerShell/Scripts/check-subnet-mask.ps1

40 lines
1009 B
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-08-27 15:35:34 +02:00
.SYNOPSIS
2021-09-24 17:19:49 +02:00
Checks the given subnet mask for validity
2021-10-04 21:29:23 +02:00
.DESCRIPTION
check-subnet-mask.ps1 [<address>]
2021-08-27 15:35:34 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-subnet-mask 255.255.255.0
2021-10-04 17:42:06 +02:00
subnet mask 255.255.255.0 is valid
2021-08-27 15:35:34 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-29 17:50:03 +02:00
Author: Markus Fleschutz · License: CC0
2021-08-27 15:35:34 +02:00
#>
param([string]$address = "")
function IsSubNetMaskValid { param([string]$IP)
$RegEx = "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(255|254|252|248|240|224|192|128|0)$"
if ($IP -match $RegEx) {
return $true
} else {
return $false
}
}
try {
if ($address -eq "" ) { $address = read-host "Enter subnet mask to validate" }
if (IsSubNetMaskValid $address) {
"✔️ subnet mask $Address is valid"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-08-27 15:35:34 +02:00
} else {
write-warning "Invalid subnet mask: $address"
exit 1
}
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-08-27 15:35:34 +02:00
exit 1
}