forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb7ff03
commit 85bf472
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 '.' | ||
} | ||
} | ||
} |