-
Notifications
You must be signed in to change notification settings - Fork 0
/
az-get-all-subnet.ps1
87 lines (53 loc) · 2.03 KB
/
az-get-all-subnet.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
87
$dataFile = New-Item -Path $env:USERPROFILE -Name "azureVnet-$(get-date -Format ddMMyyyy-hhmmss).csv" -Force
$vnetInfoCollection = @()
Get-AzSubscription | Foreach-Object {
$sub = Set-AzContext -SubscriptionId $_.SubscriptionId
$vnets = Get-AzVirtualNetwork
foreach ($vnet in $vnets) {
$vnetInfo = [PSCustomObject]@{
Subscription = $sub.Subscription.Name
Name = $vnet.Name
Vnet = $vnet.AddressSpace.AddressPrefixes
Subnets = $vnet.Subnets.AddressPrefix
}
$vnetInfoCollection += $vnetInfo
}
}
$arrCountVnets = @()
$arrCountSubnets = @()
# Loop through each object and append count into the array
foreach ($object in $vnetInfoCollection) {
$countVnet = $object.Vnet.Count
$arrCountVnets += [int]$countVnet
$countSubnet = $object.Subnets.Count
$arrCountSubnets += [int]$countSubnet
}
# Get max value from array
$maxCountVnet = [int]($arrCountVnets | Measure -Maximum).Maximum
$maxCountSubnet = [int]($arrCountSubnets | Measure -Maximum).Maximum
# Loop through the result and get attribute inside each object
foreach ($object in $vnetInfoCollection) {
$objVnet = [PSCustomObject]@{
Subscription = $object.Subscription
Name = $object.Name
}
# create new object and append vnet into the objVnet
for ($i = 0; $i -lt $maxCountVnet; $i++) {
$keyName = "Vnet$($i)"
$objVnet | Add-Member NoteProperty $keyName $object.Vnet[$i]
}
# create new object and append subnet into the objVnet
if ($object.Subnets.Count -lt 2) {
$keyName = "Subnet0"
$objVnet | Add-Member NoteProperty $keyName $object.Subnets
}
else {
for ($i = 0; $i -lt $maxCountSubnet; $i++) {
$keyName = "Subnet$($i)"
$objVnet | Add-Member NoteProperty $keyName $object.Subnets[$i]
}
}
# output data to file and put some text on the console
$objVnet | Export-Csv -Path $dataFile.FullName -Force -Append -NoTypeInformation
Write-Output $objVnet
}