Creating IP segment lists

by May 6, 2011

If you need a list of consecutive IP addresses, you can check out this function. You can see that it takes a start and an end address and then returns all IP addresses in between:

function New-IPSegment ($start, $end) {
  $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 '.'
  }
}

 

 

Twitter This Tip!
ReTweet this Tip!