-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGet-ARMVmStatus.ps1
More file actions
100 lines (88 loc) · 3.42 KB
/
Get-ARMVmStatus.ps1
File metadata and controls
100 lines (88 loc) · 3.42 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
function Get-ARMVmStatus {
<#
.SYNOPSIS
Obtains Azure VM runtime status
.DESCRIPTION
Get-ARMVmStatus is a PowerShell function that makes it easier to obtain the run status of selected or all VMs. The -ResourcGroup parameter scopes your query to a particular resource group. The -All switch parameter retrieves VM status across all resource groups within the active Azure subscription. Be careful with this option because your request will be throttled by the Azure platform if you have a lot of VMs (you're considered to be 'hammering' the Resource Manager Compute APIs too hard).
.PARAMETER IPAddress
The IPAddress(es) to return the Geolocation information for.
.EXAMPLE
Get-ARMVmStatus -ResourceGroup 'myRG'
.EXAMPLE
Get-ARMVmStatus -All
.EXAMPLE
vms -res 'myrg1'
.INPUTS
String
.OUTPUTS
PSCustomObject
.NOTES
Author: Timothy Warner
Website: timwarnertech.com
Twitter: @TechTrainerTim
Credit: I adapted code from a few different sources. Thanks to those developers for the "leg up": http://timw.info/s01; http://timw.info/s02
#>
[CmdletBinding(DefaultParameterSetName = 'default',
ConfirmImpact = 'low')]
[Alias("vms")]
Param (
[Parameter(ParameterSetName = 'default')]
[string]$ResourceGroup,
[Parameter(ParameterSetName = 'All')]
[switch]$All
)
Begin {
# test for RG existence
if ($All -eq $False) {
$rggroup = Get-AzureRmResourceGroup
foreach ($rgz in $rggroup) {
if ($rggroup.ResourceGroupName -notcontains $ResourceGroup) {
throw "$ResourceGroup is not a valid resource group name in the current subscription."
}
}
}
}
Process {
if ($All) {
$RGs = Get-AzureRMResourceGroup
foreach($RG in $RGs) {
$VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName
foreach($VM in $VMs) {
$VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status
$RGN = $VMDetail.ResourceGroupName
foreach ($VMStatus in $VMDetail.Statuses) {
if($VMStatus.Code -like ("PowerState/*")) {
$VMStatusDetail = $VMStatus.DisplayStatus
}
$out = [PSCustomObject]@{
ResourceGroup = $RGN
Name = $VM.Name
Status = $VMStatusDetail
}
}
$out | Select-Object -Unique | Sort-Object -Property ResourceGroup
}
}
}
else {
$name = '*'
Get-AzureRmVM -ResourceGroupName $ResourceGroup |
Get-AzureRmVM -Status |
Select-Object -Property Name, Statuses |
Where-Object -FilterScript {$_.Name -like $Name} |
ForEach-Object {
$VMName = $_.Name
$_.Statuses |
Where-Object {$_.Code -like 'PowerState/*'} |
ForEach-Object {
$props = [ordered]@{
ResourceGroup = $ResourceGroup
Name = $VMName
Status = $_.DisplayStatus
}
New-Object -TypeName PSCustomObject -Property $props
}
}
}
}
}