Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace System.Collections.Generic
- function IPRange-ToList{
- Param ( [Parameter(Mandatory=1,HelpMessage='Starting IP Address',ValueFromPipeline=1)][ipaddress]$Start,
- [Parameter(Mandatory=1,HelpMessage='Ending IP Address' ,ValueFromPipeline=1)][ipaddress]$End
- )
- $s_oct = [PSCustomObject] @{
- a = $Start.GetAddressBytes()[0]
- b = $Start.GetAddressBytes()[1]
- c = $Start.GetAddressBytes()[2]
- d = $Start.GetAddressBytes()[3]
- ab = $Start.GetAddressBytes()[0..1] -join '.'
- abc = $Start.GetAddressBytes()[0..2] -join '.'
- }
- $e_oct = [PSCustomObject] @{
- a = $End.GetAddressBytes()[0]
- b = $End.GetAddressBytes()[1]
- c = $End.GetAddressBytes()[2]
- d = $End.GetAddressBytes()[3]
- ab = $End.GetAddressBytes()[0..1] -join '.'
- abc = $End.GetAddressBytes()[0..2] -join '.'
- }
- $ip_list = [System.Collections.Generic.List[PSObject]]::new()
- # check if there is a difference in the c octets.
- if($s_oct.c -eq $e_oct.c){
- $s_oct.d..$e_oct.d | ForEach-Object { $ip_list.Add($($s_oct.abc + ".$_")) }
- }else{
- # add 1st range
- $s_oct.d..255 | ForEach-Object { $ip_list.Add($($s_oct.abc + ".$_")) }
- # check if the difference between the c octets is greater than 1
- #if greater, sequencial loop adding ranges
- if([math]::Abs($s_oct.c - $e_oct.c) -gt 1){
- $tmp = $s_oct.c + 1
- while($tmp -lt $e_oct.c){
- 1..255 | ForEach-Object { $ip_list.Add($($s_oct.ab + ".$tmp.$_")) }
- $tmp += 1
- }
- }
- # add last range
- 1..$e_oct.d | ForEach-Object { $ip_list.Add($($e_oct.abc + ".$_")) }
- }
- return $ip_list
- }
- Clear-Host
- IPRange-ToList '192.168.0.100' '192.168.2.10'
- IPRange-ToList -Start '10.10.1.1' -End '10.10.2.50'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement