PowerShell/scripts/check-subnet-mask.ps1

42 lines
1.0 KiB
PowerShell
Raw Normal View History

2023-10-31 11:25:11 +01: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
2022-01-29 12:47:46 +01:00
This PowerShell script checks the given subnet mask for validity.
2021-10-16 16:50:10 +02:00
.PARAMETER address
Specifies the subnet mask to check
2021-08-27 15:35:34 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-subnet-mask.ps1 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
2022-09-06 21:42:04 +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 {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-08-27 15:35:34 +02:00
exit 1
}