This first function is helpful to provide a range of IPv4 addresses within a set range
function New-IPRange ($start, $end) { # created by Dr. Tobias Weltner, MVP PowerShell $ip1 = ([System.Net.IPAddress]$start).GetAddressBytes() [Array]::Reverse($ip1) $ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address $ip2 = ([System.Net.IPAddress]$end).GetAddressBytes() [Array]::Reverse($ip2) $ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address for ($x=$ip1; $x -le $ip2; $x++) { $ip = ([System.Net.IPAddress]$x).GetAddressBytes() [Array]::Reverse($ip) $ip -join '.' } }
New-IPRange 192.168.10.10 192.168.10.20
New IP Range 192.168.10.10 192.168.10.11 192.168.10.12 192.168.10.13 192.168.10.14 192.168.10.15 192.168.10.16 192.168.10.17 192.168.10.18 192.168.10.19 192.168.10.20
This next function will get the broadcast IPv4 address from a CIDR range
function Get-Broadcast ($addressAndCidr) { $addressAndCidr = $addressAndCidr.Split("/") $addressInBin = (New-IPv4toBin $addressAndCidr[0]).ToCharArray() for($i=0;$i -lt $addressInBin.length;$i++) { if($i -ge $addressAndCidr[1]) { $addressInBin[$i] = "1" } } [string[]]$addressInInt32 = @() for ($i = 0;$i -lt $addressInBin.length;$i++) { $partAddressInBin += $addressInBin[$i] if(($i+1)%8 -eq 0) { $partAddressInBin = $partAddressInBin -join "" $addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2) $partAddressInBin = "" } } $addressInInt32 = $addressInInt32 -join "." return $addressInInt32 }
Get-Broadcast 192.168.10.10/27
New IP Range Broadcast 192.168.10.31
Next this function will detect if a specified IPv4 address is in the range
function Test-IPinIPRange ($Address,$Lower,$Mask) { [Char[]]$a = (New-IPv4toBin $Lower).ToCharArray() if($mask -like "*.*") { [Char[]]$b = (New-IPv4toBin $Mask).ToCharArray() } else { [Int[]]$array = (1..32) for($i=0;$i -lt $array.length;$i++) { if($array[$i] -gt $mask){$array[$i]="0"}else{$array[$i]="1"} } [string]$mask = $array -join "" [Char[]]$b = $mask.ToCharArray() } [Char[]]$c = (New-IPv4toBin $Address).ToCharArray() $res = $true for($i=0;$i -le $a.length;$i++) { if($a[$i] -ne $c[$i] -and $b[$i] -ne "0") { $res = $false } } return $res }
Write-Output "`r`nTest If IP In Range - 192.168.23.128/25" Test-IPinIPRange "192.168.23.200" "192.168.23.12" "255.255.255.128" Write-Output "`r`nTest If IP In Range - 192.168.23.127/24" Test-IPinIPRange "192.168.23.127" "192.168.23.12" "24"
Test If IP In Range - 192.168.23.128/25 False Test If IP In Range - 192.168.23.127/24 True
This next function will convert an IPv4 address to a Bin
function New-IPv4toBin ($ipv4) { $BinNum = $ipv4 -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')} return $binNum -join "" }
Write-Output "`r`nIP To Bin" New-IPv4toBin 192.168.10.10
IP To Bin 11000000101010000000101000001010
This last function will convert a Bin to an IPv4 address
function New-IPv4fromBin($addressInBin) { [string[]]$addressInInt32 = @() $addressInBin = $addressInBin.ToCharArray() for ($i = 0;$i -lt $addressInBin.length;$i++) { $partAddressInBin += $addressInBin[$i] if(($i+1)%8 -eq 0) { $partAddressInBin = $partAddressInBin -join "" $addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2) $partAddressInBin = "" } } $addressInInt32 = $addressInInt32 -join "." return $addressInInt32 }
Write-Output "`r`nIP From Bin - 192.168.23.250" New-IPv4fromBin "11000000101010000001011111111010" Write-Output "`r`nIP From Bin - 192.168.10.10" New-IPv4fromBin "11000000101010000000101000001010"
IP From Bin - 192.168.23.250 192.168.23.250 IP From Bin - 192.168.10.10 192.168.10.10
Write-Output "`r`nIP CIDR to Range" New-IPRange "192.168.23.120" (Get-Broadcast "192.168.23.120/25")
IP CIDR to Range 192.168.23.120 192.168.23.121 192.168.23.122 192.168.23.123 192.168.23.124 192.168.23.125 192.168.23.126 192.168.23.127
References: http://ficility.net/2013/03/16/powershell-example-how-to-work-with-the-ip-addresses-ipv4/