-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGet-AvailableIPRanges.ps1
86 lines (77 loc) · 3.2 KB
/
Get-AvailableIPRanges.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Function Get-AvailableIPRanges {
param (
[Parameter(Mandatory=$false)]
[ValidatePattern('\d{1,3}\.\d{1,3}\.\d{1,3}\.0')]
[string]$IPAddress,
[switch]$Available
)
# Retrieve current network ranges and their names
$networks = Get-LabVirtualNetwork | Select-Object Name, @{Name='Network'; Expression={ $_.AddressSpace.Network.AddressAsString }}
# Get the subnet prefix for created networks
If ($null -eq $IPAddress) {
$SubnetPrefix = ($networks | Where-Object { $_.Name -ne "Default Switch" } | Select-Object -First 1).Network.Split('.')[0..1] -join '.'
} else {
$SubnetPrefix = $IPAddress.Split('.')[0..1] -join '.'
}
# Define the total possible X values that are divisible by 10
$totalXValues = 0..255 | Where-Object { $_ % 10 -eq 0 }
# Initialize an array to hold the subnet status objects
$subnetStatus = @()
# Loop through each possible X value
foreach ($x in $totalXValues) {
# Construct the subnet address
$subnet = "$SubnetPrefix.$x.0"
# Check if the subnet is in use
$inUse = $networks | Where-Object { $_.Network -eq $subnet }
if ($IPAddress) {
# If IP address parameter is given, check if it matches the current subnet
if ($subnet -eq $IPAddress) {
if ($inUse) {
# If the subnet is in use, return the resource name
return [PSCustomObject]@{
Subnet = $subnet
Availability = $inUse.Name
}
} else {
# If the subnet is not in use, return 'Available'
return [PSCustomObject]@{
Subnet = $subnet
Availability = 'Available'
}
}
}
} else {
# If IP address parameter is not given and "available" switch is used, add only available subnets to the array
if ($Available) {
if (-not $inUse) {
$status = [PSCustomObject]@{
Subnet = $subnet
Availability = 'Available'
}
$subnetStatus += $status
}
} else {
# If IP address parameter is not given and "available" switch is not used, add all subnets to the array
if ($inUse) {
# If the subnet is in use, add it with the resource name
$status = [PSCustomObject]@{
Subnet = $subnet
Availability = $inUse.Name
}
} else {
# If the subnet is not in use, mark it as available
$status = [PSCustomObject]@{
Subnet = $subnet
Availability = 'Available'
}
}
# Add the status object to the array
$subnetStatus += $status
}
}
}
# Return the subnet status array if IP address parameter is not given
if (-not $IPAddress) {
return $subnetStatus
}
}