-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzureRMVMState.ps1
57 lines (45 loc) · 1.73 KB
/
Get-AzureRMVMState.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
<#
.SYNOPSIS
Returns Status of the Azure RM VM
.DESCRIPTION
This utility runbook determines the current status of the Azure RM VM and returns the value back to the caller
.PARAMETER ResourceGroupName
Name of the resource group where the VM is located.
.PARAMETER VMName
Name of the VM that you want to connect to
.EXAMPLE
CheckAzureRMVMPowerState -ResourceGroupName "RG1" -VMName "VM01"
.Notes
Author: Arjun Bahree
E-mail: [email protected]
Creation Date: 13/Dec/2017
Last Revision Date: 13/Dec/2017
Version: 1.0
Development Environment: Azure Automation Runbook Editor and VS Code IDE
PS Version: 5.1
Platform: Windows
#>
param(
[Parameter(Mandatory=$true)]
[String]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[String]$VMName,
[Parameter(Mandatory=$true)]
[ValidateSet('PowerState','ProvisioningState')]
[String]$State
)
if (!(Get-AzureRmContext).Account){
Write-Error "You need to be logged into your Azure Subscription using PowerShell cmdlet 'Login-AzureRmAccount' with a valid Azure Organization Id (and not @outlook.com or any other Microsoft Live Id) having required permissions to the Azure Automation Account and Resource Group"
return
}
# Get current status of the VM in context fpr the Input State
$vmstatus = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Status
# Check the current staus of VM in context for the Input State
foreach ($vstatus in $vmstatus.Statuses)
{
if ($vstatus.Code.Contains($State))
{
return $vstatus.Code.split('/')[1]
}
}
return