Simples PowerShell Beispiel wie man prüfen kann, ob eine bestimmte IP Adresse innerhalb einer bestimmten IP Range ist.
# Test if IP is within IP range
Function Test-IsIpAddressInRange {
param(
[string] $IpAddress,
[string] $IpRangeStart,
[string] $IpRangeEnd
)
$ip = [system.net.ipaddress]::Parse($ipAddress).GetAddressBytes()
[array]::Reverse($ip)
$ip = [system.BitConverter]::ToUInt32($ip, 0)
$start = [system.net.ipaddress]::Parse($IpRangeStart).GetAddressBytes()
[array]::Reverse($start)
$start = [system.BitConverter]::ToUInt32($start, 0)
$end = [system.net.ipaddress]::Parse($IpRangeEnd).GetAddressBytes()
[array]::Reverse($end)
$end = [system.BitConverter]::ToUInt32($end, 0)
$Result = $start -le $ip -and $ip -le $end
Write-Host "Test-IsIpAddressInRange -IpAddress $IpAddress -IpRangeStart $IpRangeStart -IpRangeEnd $IpRangeEnd"
Return $Result
}
# Example to check all IPsTest if IP is within IP range
# Get all local NICs with IPv4 address an check if within range 192.168.0.0-192.168.255.255
$Nics = Get-NetAdapter | Get-NetIPConfiguration | Get-NetIPAddress | Where-Object {$_.AddressFamily -eq "IPv4"}
foreach ($NIC in $NICs)
{
Test-IsIpAddressInRange -IpAddress $NIC.IPAddress -IpRangeStart "192.168.0.0" -IpRangeEnd "192.168.255.255"