I have the following powershell function, and I want to return the IP range from a CIDR.
Here is the code:
function ConvertFrom-CidrNotation {
param(
[Parameter(Mandatory=$true)]
[string]$cidr
)
$parts = $cidr -split "/"
$ip = $parts[0]
$prefix = $parts[1]
$subnet = [math]::pow(2, (32 - $prefix))
$startIP = ([System.Net.IPAddress]::Parse($ip).GetAddressBytes() | ForEach-Object{ $_ -bor 0 }) -join "."
# Correct the end IP calculation
$ipBytes = [System.Net.IPAddress]::Parse($ip).GetAddressBytes()
$subnetBytes = [BitConverter]::GetBytes([int]$subnet)
$endIPBytes = for ($i = 0; $i -lt 4; $i++) { $ipBytes[$i] -bor $subnetBytes[$i] }
$endIP = $endIPBytes -join "."
return "$startIP - $endIP"
}
Please, can you tell me why I get wrong results?
Use following to get start address