Created
July 23, 2020 05:45
-
-
Save rmbolger/aa6c46e0cdac48034da6a70f03497b0b to your computer and use it in GitHub Desktop.
Test-IPInSubnet.ps1
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-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