-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzNetViz.ps1
More file actions
255 lines (237 loc) · 13.2 KB
/
AzNetViz.ps1
File metadata and controls
255 lines (237 loc) · 13.2 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Param(
[Parameter(Mandatory)]
[string]$outputFile,
[ValidateSet("svg","png","jpg","gif","imap","cmapx","jp2","json","pdf","plain","dot")]
[string]$outputFormat = "svg",
[ValidateSet("Hierarchical","radial","circular","SpringModelSmall","SpringModelMedium","SpringModelLarge","fdp","sfdp","neato","dot","twopi","circo","patchwork","nop")]
[string]$layout = "Hierarchical"
)
Import-Module PSGraph
$currentCon = Get-AzContext
$subscriptions = Get-AzSubscription -TenantId $currentCon.Tenant.Id
$outputFile = "$($outputFile).$($outputFormat)"
$subVnets = @()
$subErc = @()
$subVngs = @()
$subVngConns = @()
$subLngs = @()
$subVwan = @()
$subVhubs = @()
$subVhubErcGw = @()
$subVhubErcConns = @()
$subVhubVpnGw = @()
foreach ($sub in $subscriptions) {
if ($sub.SubscriptionPolicies.QuotaId -notmatch "MSDN_2014-09-01|PayAsYouGo_2014-09-01|AAD_2015-09-01|FreeTrial_2014-09-01|AzurePass_2014-09-01") {
Select-AzSubscription $sub.Id | Out-Null
$thisSubVnets = Get-AzVirtualNetwork | Select-Object Name, ResourceGroupName, location, Type, VirtualNetworkPeerings, Subnets, DhcpOptions, AddressSpace, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
$thisSubResources = Get-AzResource
$subVnets += $thisSubVnets
foreach ($vnet in $thisSubVnets) {
$subVngList = ($vnet.Subnets | Where-Object Name -eq 'GatewaySubnet').Ipconfigurations.Id
foreach ($vng in $subVngList) {
$thisSubVngs = Get-AzVirtualNetworkGateway -ResourceGroupName $vng.Split("/")[4] -Name $vng.Split("/")[8] | Select-Object Name, Id, ResourceGroupName, location, Type, GatewayType, IpConfigurations, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
$subVngs += $thisSubVngs
$vng1text = "`"$($vng.Split("/ipCon")[0])`""
$subVngConns += Get-AzVirtualNetworkGatewayConnection -ResourceGroupName $vng.Split("/")[4] | Where-Object VirtualNetworkGateway1Text -eq $vng1text
}
}
foreach ($thisLngs in ($thisSubResources | Where-Object ResourceType -eq "Microsoft.Network/localNetworkGateways")) {
$subLngs += Get-AzLocalNetworkGateway -ResourceGroupName $thisLngs.ResourceGroupName -Name $thisLngs.Name | Select-Object Name, Id, ResourceGroupName, location, Type, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
}
$subErc += Get-AzExpressRouteCircuit -WarningAction silentlyContinue | Select-Object Name, Id, ResourceGroupName, location, Type, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
$thisSubVwan = Get-AzVirtualWan | Select-Object Name, ResourceGroupName, Id, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
$subVwan += $thisSubVwan
$vhubRgs = [System.Collections.ArrayList]::new()
foreach ($vWan in $thisSubVwan) {
$vhubRgs += $vwan.ResourceGroupName
}
$vhubRgs = $vhubRgs | Sort-Object | Select-Object -Unique
foreach ($vHubRg in $vhubRgs) {
$thisSubVhubs = Get-AzVirtualHub -ResourceGroupName $vHubRg | Select-Object Name, Id, ResourceGroupName, location, Type, VirtualWan, AzureFirewall, ExpressRouteGateway, VpnGateway, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
$subVhubs += $thisSubVhubs
foreach ($vHub in $thisSubVhubs) {
if ($vHub.VpnGateway.id) {
$vpnGw = $vHub.VpnGateway.id.Split("/")
$subVhubVpnGw += Get-AzVpnGateway -ResourceGroupName $vpnGw[4] -Name $vpnGw[8] | Select-Object Name, Id, ResourceGroupName, location, Type, @{ name='SubscriptionId'; expr={$_.Id.Split('/')[2]} }
}
if ($vHub.ExpressRouteGateway.id) {
$ercGw = $vHub.ExpressRouteGateway.id.Split("/")
$thisVhubErcGw = @()
$thisVhubErcGw = Get-AzExpressRouteGateway -ResourceGroupName $ercGw[4] -Name $ercGw[8]
$subVhubErcGw += $thisVhubErcGw
$subVhubErcConns += Get-AzExpressRouteConnection -ResourceGroupName $thisVhubErcGw.ResourceGroupName -ExpressRouteGatewayName $thisVhubErcGw.Name
}
# TODO Add P2S VPN Gateway connections if we ever have them
}
}
}
}
Select-AzSubscription $currentCon.Subscription | Out-Null
$output = @()
function SetDhcp ($setDhcp) {
if ($setDhcp.DhcpOptions) {
$dnsOut = ""
$setDhcp.DhcpOptions.DnsServers | Foreach-Object {
$dnsOut += "$_ "
}
if ($dnsOut.Length -lt 7) {
$dnsOut = "Azure DNS"
}
} else {
$dnsOut = "Azure DNS"
}
return $dnsOut
}
function SetAddress ($setAddress) {
if ($setAddress.AddressSpace) {
$addressOut = ""
$setAddress.AddressSpace.AddressPrefixes | Foreach-Object {
$addressOut += "$_ "
}
if ($addressOut.Length -lt 7) {
$addressOut = "Misconfigured"
}
} else {
$addressOut = "Misconfigured"
}
return $addressOut
}
function LogOutput ($message) {
$output = "[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss.fff'))] : $message"
Write-Output $output
}
# ======== Graph Generation ========
$sgcount = 0
$vwcount = 1000
graph network {
node @{shape="box"}
# ======== Node loops ========
foreach ($sub in $subscriptions) {
if ((($subVwan | Where-Object SubscriptionId -eq $sub.Id).count) + (($subErc | Where-Object SubscriptionId -eq $sub.Id).count) + (($subLngs | Where-Object SubscriptionId -eq $sub.Id).count) + (($subVngs | Where-Object SubscriptionId -eq $sub.Id).count) + (($subVnets | Where-Object SubscriptionId -eq $sub.Id).count) -gt 0) {
SubGraph $sgcount -Attributes @{style='filled';color='lightgrey';label=$sub.Name} {
foreach ($vnet in $subVnets | Sort-Object VirtualNetworkPeerings -Descending | Where-Object SubscriptionId -eq $sub.Id) {
if (!($vnet.Name -eq "workers-vnet")) {
$vnetDhcp = SetDhcp $vnet
$vnetAddress = SetAddress $vnet
Entity -Name $vnet.Name -Show value @{
resourcegroup = $vnet.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq $vnet.SubscriptionId).Name
location = $vnet.location
type = $vnet.Type
ipconnected = $vnet.Subnets.IpConfigurations.count
addressspace = $vnetAddress
dns = $vnetDhcp
}
}
}
foreach ($vwan in $subVwan | Where-Object SubscriptionId -eq $sub.Id) {
SubGraph $vwcount -Attributes @{style='filled';color='darkgrey';label=$vwan.Name} {
foreach ($vHub in $subVhubs | where-object {$_.VirtualWan.Id -eq $vwan.Id}) {
$vwanName = $vHub.VirtualWan.Id.Split("/")[8]
if ($vHub.AzureFirewall.Id) {
$azFirewallStatus = "enabled"
} else {
$azFirewallStatus = "disabled"
}
Entity -Name $vHub.name -Show value @{
resourcegroup = $vHub.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq ($vHub.Id.Split("/")[2])).Name
location = $vHub.location
type = $vHub.Type
azFirewall = $azFirewallStatus
virtualWan = $vwanName
}
foreach ($vhubVpn in $subVhubVpnGw | Where-Object {$_.VirtualHub.Id -eq $vHub.Id}) {
$vpnLinkName = $vhubVpn.Connections[0].name.Split("Connection-")[1]
Entity -Name $vpnLinkName -Show value @{
resourcegroup = $vHub.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq ($vHub.Id.Split("/")[2])).Name
location = $vHub.location
type = "Microsoft.Network/vpnGateways/vpnConnections"
}
}
}
}
$vwcount++
}
foreach ($erc in $subErc | Where-Object SubscriptionId -eq $sub.Id) {
Entity -Name $erc.name -Show value @{
resourcegroup = $erc.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq ($erc.Id.Split("/")[2])).Name
location = $erc.location
type = $erc.Type
}
}
foreach ($lng in $subLngs | Where-Object SubscriptionId -eq $sub.Id) {
Entity -Name $lng.name -Show value @{
resourcegroup = $lng.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq ($lng.Id.Split("/")[2])).Name
location = $lng.location
type = $lng.Type
}
}
foreach ($vng in $subVngs | Where-Object SubscriptionId -eq $sub.Id) {
Entity -Name $vng.name -Show value @{
resourcegroup = $vng.ResourceGroupName
subscription = ($subscriptions | Where-Object Id -eq ($vng.Id.Split("/")[2])).Name
location = $vng.location
type = $vng.Type
gatewayType = $vng.GatewayType
}
}
}
$sgcount++
}
}
# ======== Connection loops ========
$peerLog = [System.Collections.ArrayList]::new()
foreach ($vnetConn in $subVnets | Sort-Object VirtualNetworkPeerings -Descending) {
foreach ($peering in $vnetConn.VirtualNetworkPeerings) {
$peervNet = $subVnets | Where-Object Id -eq $peering.RemoteVirtualNetwork.Id
if ($peervNet) {
$peerName = $peervNet.Name
} else {
$peerName = (($peering.RemoteVirtualNetwork.Id).Split("/"))[8]
if ($peername -like "HV_*") {
$vHubname1 = $peerName.Split("_")[1] # This is required because the auto-generated name for vWan Hub peers is weird
$vHubname2 = $vHubname1.Substring(0,($vHubname1.Length)-1)
$peerName = ($subVhubs | Where-Object Name -like "$($vHubname2)*").Name
}
}
if (!(($peerLog.Contains("$($peerName) -> $($vnetConn.Name)")) -or ($peerName -eq "workers-vnet") -or ($vnetConn.Name -eq "workers-vnet"))) {
Edge -From $vnetConn.Name -To $peerName @{label="peering";arrowhead="none"}
$peerLog += "$($vnetConn.Name) -> $($peerName)"
# $peerLog.Add("$($vnetConn.Name) -> $($peerName)")
}
}
}
foreach ($vngVnetConn in $subVngs) {
$vngVnetId = ($vngVnetConn.IpConfigurations.subnet | Where-Object id -like "*GatewaySubnet*").Id
$vngVnet = ($vngVnetId.Split("/"))[8]
Edge -From $vngVnetConn.name -To $vngVnet @{label="gateway";arrowhead="none"}
}
foreach ($vngConn in $subVngConns) {
if ($vngConn.VirtualNetworkGateway2) {
Edge -From $vngConn.VirtualNetworkGateway2.Id.Split("/")[8] -To $vngConn.VirtualNetworkGateway1.Id.Split("/")[8] @{label="connection";arrowhead="none"}
} elseif ($vngConn.LocalNetworkGateway2) {
Edge -From $vngConn.LocalNetworkGateway2.Id.Split("/")[8] -To $vngConn.VirtualNetworkGateway1.Id.Split("/")[8] @{label="connection";arrowhead="none"}
} elseif ($vngConn.Peer) {
Edge -From $vngConn.Peer.Id.Split("/")[8] -To $vngConn.VirtualNetworkGateway1.Id.Split("/")[8] @{label="connection";arrowhead="none"}
}
}
foreach ($vHub in $subVhubs) {
#foreach ($vnetConn in $vHub.VirtualNetworkConnections) {
# TODO Add any missing vNet connections that may exist
#}
$vhubVpns = $subVhubVpnGw | Where-Object {$_.VirtualHub.Id -eq $vHub.Id}
foreach ($vhubVpn in $vhubVpns) {
$vpnLinkName = $vhubVpn.Connections[0].name.Split("Connection-")[1]
Edge -From $vHub.Name -To $vpnLinkName @{label="vpnlink";arrowhead="none"}
}
$vhubErcConns = $subVhubErcConns | Where-Object {$_.RoutingConfiguration.AssociatedRouteTable.Id.Split("/")[8] -eq $vHub.Name }
foreach ($vhubErcConn in $vhubErcConns) {
Edge -From $vHub.Name -To $vhubErcConn.ExpressRouteCircuitPeering.Id.Split("/")[8] @{label="connection";arrowhead="none"}
}
} #>
} | Export-PSGraph -DestinationPath $outputFile -OutputFormat $outputFormat -LayoutEngine $layout
$peerLog.count