Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace System.Collections.Generic
- function IPRange-ToArray{
- [CmdletBinding()]
- [OutputType([psobject])]
- param(
- [Parameter(
- Mandatory = $true,
- HelpMessage = 'Starting IP Address',
- ValueFromPipeline = $true,
- Position = 0
- )]
- [ipaddress]$Start,
- [Parameter(
- Mandatory = $true,
- HelpMessage = 'Ending IP Address',
- ValueFromPipeline = $true,
- Position = 1
- )]
- [ipaddress]$End
- ) #param block 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-ToArray '192.168.0.100' '192.168.2.10'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement