forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
New-IPRange.ps1
67 lines (51 loc) · 1.58 KB
/
New-IPRange.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 '.'
}
}
}