forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzureRmVmPublicIP.ps1
224 lines (193 loc) · 8.17 KB
/
Get-AzureRmVmPublicIP.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
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
Function Get-AzureRmVmPublicIP {
<#
.SYNOPSIS
Correlate AzureRM VMs, NetworkInterfaces, and Public IPs
.DESCRIPTION
Correlate AzureRM VMs, NetworkInterfaces, and Public IPs
Prerequisites:
* You have the AzureRM module
* You're authenticated
* You're running PowerShell 4 or later
.PARAMETER ResourceGroupName
Query this resource group
.PARAMETER VMName
One or more VM names to include. Accepts wildcards. Defaults to all.
.PARAMETER IncludeObjects
If specified, include VM, NIC, and PIP (Public IP) properties on each entry
.PARAMETER VMStatus
If specified, the VM property from IncludeObjects will include data from Get-AzureRmVm '-Status'
Using this switch will trigger IncludeObjects
.EXAMPLE
Login-AzureRmAccount
Get-AzureRmVmPublicIP -ResourceGroupName 'my-resource-group'
# VMName NICName PublicIP
# ------ ------- --------
# VM-2 VM-2-NIC 23.96.1.2
# VM-3 VM-3-NIC 23.96.1.3
# VM-4 VM-4-NIC 168.61.2.1
# VM-16 VM-16-NIC 168.61.10.27
# VM-17 VM-17-NIC 23.96.17.56
# VM-18 VM-18-NIC 23.96.19.71
# VM-1 VM-1-NIC Not Assigned
# List VMs, NICS, and Public IPs in 'my-resource-group'
.EXAMPLE
Login-AzureRmAccount
Get-AzureRmVmPublicIP -ResourceGroupName 'my-resource-group' -VMName VM-1* -IncludeObjects
# ...
# VMName : VM-18
# NICName : VM-18-NIC
# PublicIP : 23.96.19.71
# VM : Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine
# NIC : Microsoft.Azure.Commands.Network.Models.PSNetworkInterface
# PIP : Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress
# VMName : VM-1
# NICName : VM-1-NIC
# PublicIP : Not Assigned
# VM : Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine
# NIC : Microsoft.Azure.Commands.Network.Models.PSNetworkInterface
# PIP : Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress
# Get VMs, NICs, and Public IPs in 'my-resource-group'
# with name like VM-1*
# Include the VM, Network Interface (NIC), and Public IP (PIP) objects properties
.EXAMPLE
$Details = Get-AzureRmVmPublicIP -ResourceGroupName 'my-resource-group' -VMStatus
$Details[0]
# VMName : VM-18
# NICName : VM-18-NIC
# PublicIP : 23.96.19.71
# VM : Microsoft.Azure.Commands.Compute.Models.PSVirtualMachineInstanceView <<<<
# NIC : Microsoft.Azure.Commands.Network.Models.PSNetworkInterface
# PIP : Microsoft.Azure.Commands.Network.Models.PSPublicIpAddress
# All sorts of data to explore in the VM property. Output from Get-AzureRmVm -Name <ThisVm> -Status
$Details[0].VM.VMAgent.ExtensionHandlers
# Type TypeHandlerVersion Status
# ---- ------------------ ------
# Microsoft.Azure.Diagnostics.IaaSDiagnostics 1.7.1.0 Microsoft.Azure.Management.Compute.Models.InstanceViewStatus
# Microsoft.Compute.BGInfo 2.1 Microsoft.Azure.Management.Compute.Models.InstanceViewStatus
# Microsoft.Compute.CustomScriptExtension 1.8 Microsoft.Azure.Management.Compute.Models.InstanceViewStatus
.EXAMPLE
Get-AzureRmVmPublicIP -ResourceGroupName $ResourceGroup -VMName VM-18 -VMStatus |
Select -Property VMName,
PublicIP,
@{ label = "PrivateIP"; expression = {$_.NIC.IpConfigurations.PrivateIpAddress} },
@{ label = "VMAgentStatus"; expression = {$_.VM.VMAgent.Statuses[0].DisplayStatus} }
# VMName PublicIP PrivateIP VMAgentStatus
# ------ -------- --------- -------------
# VM-18 23.96.19.71 10.1.2.5 Ready
# Pull details from VM-18,
# extract private IP from the NIC, and the first VMAgent status we find from the VM
.FUNCTIONALITY
Azure
#>
[cmdletbinding()]
param(
[string[]]$ResourceGroupName,
[string[]]$VMName,
[switch]$IncludeObjects,
[switch]$VMStatus
)
foreach($ResourceGroup in $ResourceGroupName)
{
# Here's an absurd snippet of code to extract all VMs, NICs, and Public IPs, and correlate them together.
# From what I can tell, the Azure team didn't provide the usual pipeline support...
# This method will skip public IPs that aren't bound to a NIC, or NICs that aren't bound to a VM
Try
{
$AllVMs = @( Get-AzureRMVm -ResourceGroupName $ResourceGroup -ErrorAction Stop )
}
Catch
{
Write-Error $_
Write-Error "Could not extract VMs from resource group '$ResourceGroup'"
continue
}
Try
{
$NICS = @( Get-AzureRmNetworkInterface -ResourceGroupName $ResourceGroup -ErrorAction Stop )
}
Catch
{
Write-Error $_
Write-Error "Could not extract network interfaces from resource group '$ResourceGroup'"
continue
}
Try
{
$PublicIPS = @( Get-AzureRmPublicIpAddress -ResourceGroupName $ResourceGroup -ErrorAction Stop)
}
Catch
{
Write-Error $_
Write-Error "Could not extract public IPs from resource group '$ResourceGroup'"
continue
}
# Allow wildcard support for each name in array... filter dupes
$TheseVMs = foreach($VM in $AllVMs)
{
if($VMName)
{
foreach($Name in $VMName)
{
if($VM.Name -like $Name)
{
$VM
}
}
}
else
{
$VM
}
}
$TheseVMs = @( $TheseVMs | Sort Name -Unique )
# Correlate. Uses PS4 language.
Foreach($nic in $nics)
{
$VMs = $null
$VMs = $TheseVMs.Where({$_.Id -eq $nic.virtualmachine.id})
$PIPS = $null
$PIPS = $PublicIPS.Where({$_.Id -eq $nic.IpConfigurations.publicipaddress.id})
foreach($VM in $VMs)
{
if($VMStatus)
{
Try
{
$VMDetail = Get-AzureRMVm -ResourceGroupName $ResourceGroup -Status -Name $VM.Name -ErrorAction stop
}
Catch
{
Write-Error $_
Write-Error "Could not extract '-Status' details from $($VM.Name) in resource group $ResourceGroup. Falling back to non detailed"
$VMDetail = $VM
}
if(-not $IncludeObjects)
{
$IncludeObjects = $True
}
}
else
{
$VMDetail = $VM
}
foreach($PIP in $PIPS)
{
# Include VM, NIC, Public IP (PIP) raw objects if desired
$Output = [ordered]@{
ResourceGroupName = $ResourceGroup
VMName = $VM.Name
NICName = $nic.Name
PublicIP = $PIP.IpAddress
}
if($IncludeObjects)
{
$Output.Add('VM', $VMDetail)
$Output.Add('NIC', $NIC)
$Output.Add('PIP', $PIP)
}
[pscustomobject]$Output
}
}
}
}
}