From 85bf4726141a9e26beb5066024535ce038215654 Mon Sep 17 00:00:00 2001 From: ramblingcookiemonster Date: Mon, 13 Oct 2014 08:32:04 -0400 Subject: [PATCH] Added New-IPRange --- New-IPRange.ps1 | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 New-IPRange.ps1 diff --git a/New-IPRange.ps1 b/New-IPRange.ps1 new file mode 100644 index 00000000..e144a89b --- /dev/null +++ b/New-IPRange.ps1 @@ -0,0 +1,67 @@ +function New-IPRange { +<# +.SYNOPSIS + Returns an array of IP Addresses based on a start and end address + +.DESCRIPTION + Returns an array of IP Addresses based on a start and end address + +.PARAMETER Start + Starting IP Address + +.PARAMETER End + Ending IP Address + +.PARAMETER Exclude + Exclude addresses with this final octet + + Default excludes 0, 1, and 255 + + e.g. 5 excludes *.*.*.5 + +.EXAMPLE + New-IPRange -Start 192.168.1.5 -End 192.168.20.254 + + Create an array from 192.168.1.5 to 192.168.20.254, excluding *.*.*.[0,1,255] (default exclusion) + +.NOTES + Source: Dr. Tobias Weltner, http://powershell.com/cs/media/p/9437.aspx + +.FUNCTIONALITY + Network +#> +[cmdletbinding()] +param ( + [parameter( Mandatory = $true, + Position = 0 )] + [System.Net.IPAddress]$Start, + + [parameter( Mandatory = $true, + Position = 1)] + [System.Net.IPAddress]$End, + + [int[]]$Exclude = @( 0, 1, 255 ) +) + + #Provide verbose output. Some oddities behind casting certain strings to IP. + #Example: [ipaddress]"192.168.20500" + Write-Verbose "Parsed Start as '$Start', End as '$End'" + + $ip1 = $start.GetAddressBytes() + [Array]::Reverse($ip1) + $ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address + + $ip2 = ($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) + if($Exclude -notcontains $ip[3]) + { + $ip -join '.' + } + } +} \ No newline at end of file