-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListAllAzVMDefaultRoutes.ps1
105 lines (76 loc) · 3.14 KB
/
ListAllAzVMDefaultRoutes.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#------------------------------------------------------------------------------
#
#
# THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
#Function to List VMs with Default Route and Route details
function List-AllAzVMDefaultRoutes {
Param(
[Switch]$NextHopTypeDefault,
[Switch]$NextHopTypeother,
[Switch]$NextHopTypeany
)
$getrunningvm1 = @()
$runningNIC = @()
$runningNICRG = @()
$runningVm = @()
$testnic = @()
$testvm = @()
$getvmr = Get-AzVM -Status
foreach ($power in $getvmr ){
if($Power.powerstate -like "*running*"){ $runningNIC += $power.NetworkProfile.NetworkInterfaces.id | Split-Path -Leaf
$runningNICRG += $power.NetworkProfile.NetworkInterfaces.id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
$runningVm += $power.Name
}
}
#$runningNIC.Count
for($i=0;$i -lt $runningNIC.Count; $i++){
$getrunningnic = Get-AzNetworkInterface -Name $runningNIC[$i] -ResourceGroupName $runningNICRG[$i]
$getrunningvm1 += $getrunningnic.VirtualMachine.Id | Split-Path -Leaf
$getInternetRoute = Get-AzEffectiveRouteTable -NetworkInterfaceName $runningNIC[$i] -ResourceGroupName $runningNICRG[$i]
$testvm += $getrunningvm1
$testnic += $runningNIC
if($NextHopTypeDefault -eq $true){
$getInternetRoute2 = $getInternetRoute | Where-Object {$_.NextHopType -eq "Internet" -and $_.State -eq 'Active' -and $_.AddressPrefix -eq '0.0.0.0/0'}
if ($getInternetRoute2)
{#"VM Name: $($testvm[$i]) "
#"NIC Name: $($testnic[$i]) "
"VMName : $($getrunningvm1[$i])"
"NICName : $($getrunningnic.Name)"
$($getInternetRoute2)}
}
if($NextHopTypeother -eq $true){
$getInternetRoute2 = $getInternetRoute | Where-Object {$_.NextHopType -ne "Internet" -and $_.State -eq 'Active' -and $_.AddressPrefix -eq '0.0.0.0/0'}
if ($getInternetRoute2)
{#"VM Name: $($testvm[$i]) "
#"NIC Name: $($testnic[$i]) "
"VMName : $($getrunningvm1[$i])"
"NICName : $($getrunningnic.Name)"
$($getInternetRoute2)}
}
if($NextHopTypeany -eq $true){
$getInternetRoute2 = $getInternetRoute | Where-Object {$_.State -eq 'Active' -and $_.AddressPrefix -eq '0.0.0.0/0'}
if ($getInternetRoute2)
{#"VM Name: $($testvm[$i]) "
#"NIC Name: $($testnic[$i]) "
"VMName : $($getrunningvm1[$i])"
"NICName : $($getrunningnic.Name)"
$($getInternetRoute2)}
}
if ($NextHopTypeany -eq $false -and $NextHopTypeother -eq $false -and $NextHopTypeDefault -eq $false)
{
$getInternetRoute2 = $getInternetRoute | Where-Object {$_.State -eq 'Active' -and $_.AddressPrefix -eq '0.0.0.0/0'}
if ($getInternetRoute2)
{#"VM Name: $($testvm[$i]) "
#"NIC Name: $($testnic[$i]) "
"VMName : $($getrunningvm1[$i])"
"NICName : $($getrunningnic.Name)"
$($getInternetRoute2)}
}
}
}