-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGet-IPRange.ps1
35 lines (35 loc) · 1 KB
/
Get-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
function Global:Get-IPRange(
[string]$IPRange,
[string[]]$IPRanges = $null
) {
if ($null -ne $IPRanges) {
foreach ($Ranges in $IPRanges) {
Get-IPRange -IPRange $Ranges
}
return
}
if (-not ($IPRange -match "(^(?:(?:\d{1,3}-\d{1,3}|\d{1,3})\.){3}(?:\d{1,3}-\d{1,3}|\d{1,3})$)")) { return }
$ipOctets = $IPRange.Split('.')
$ipRangeMin = 0
$ipRangeMax = 0
$rangeFound = $false
$octetIndex = 0
foreach ($ipOctet in $ipOctets) {
$ipSplit = $ipOctet.Split('-')
if ($ipSplit.Length -eq 2) {
$ipRangeMin = [int]$ipSplit[0]
$ipRangeMax = [int]$ipSplit[1]
$rangeFound = $true
break
}
$octetIndex++
}
if ($rangeFound) {
for ($index = $ipRangeMin; $index -le $ipRangeMax; $index++) {
$ipOctets[$octetIndex] = $index
Get-IPRange -IPRange ([string]::Join('.', $ipOctets))
}
} else {
[string]::Join('.', $ipOctets)
}
}