Skip to content

Instantly share code, notes, and snippets.

@rmbolger
Created July 23, 2020 05:45
Show Gist options
  • Save rmbolger/aa6c46e0cdac48034da6a70f03497b0b to your computer and use it in GitHub Desktop.
Save rmbolger/aa6c46e0cdac48034da6a70f03497b0b to your computer and use it in GitHub Desktop.
Test-IPInSubnet.ps1
function Test-IPInSubnet {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$IP,
[Parameter(Mandatory,Position=1)]
[string[]]$Subnet
)
$IPval = ([ipaddress]$IP).Address
foreach ($cidr in $Subnet) {
$netStr, $maskStr = $cidr.Split('/')
$net = ([ipaddress]$netStr).Address
$mask = [ipaddress]::HostToNetworkOrder(-1 -shl (32 - [int]$maskStr))
if (($net -band $mask) -eq ($IPval -band $mask)) {
return $True
}
}
return $False
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment