List-AllAzVMDefaultRoutes.psm1

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
<#
 .Synopsis
  Displays List of all VMs in the Subscription with their Default Route and Route details.
 
 .REMARKS
  Displays List of all VMs in the Subscription with their Default Route and Route details.
  
 .Description
  Displays List of all VMs in the Subscription with their Default Route and Route details.
  
 .Parameter NA
  No Parameter
 
 .Switch NextHopTypeDefault
 List all VMs which have default NetHopType which is Internet
 
 .Switch NextHopTypeother
 List VMs which have any other NetHopType; NVA, Virtual network gateway, VNET
 
 .Switch NextHopTypeany
 Displays List of all VMs in the Subscription with their Default Route and Route details.
 
 .Example
  Displays List of all VMs in the Subscription with their Default Route and Route details.
  List-AllAzVMDefaultRoutes
 
  # List all VMs which have default NetHopType which is Internet
  List-AllAzVMDefaultRoutes -NextHopTypeDefault
     
  # List VMs which have any other NetHopType; NVA, Virtual network gateway, VNET
  List-AllAzVMDefaultRoutes -NextHopTypeother
 
  # Displays List of all VMs in the Subscription with their Default Route and Route details.
  List-AllAzVMDefaultRoutes -NextHopTypeany
  
#>



#------------------------------------------------------------------------------
#
#
# 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.
#
#------------------------------------------------------------------------------

#List of 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)}  


}

}

}

Export-ModuleMember -Function List-AllAzVMDefaultRoutes