-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShutdown-AzureVMs.ps1
117 lines (93 loc) · 4.74 KB
/
Shutdown-AzureVMs.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
<#
.SYNOPSIS
Invokes Stop-AzureVM for each virtual machine matching a -like pattern. Intended to be run from Azure Automation.
.PARAMETER CredentialName
The name of the credential stored in the Azure Automation assets library.
.PARAMETER Subscription
The name of the subscription within which to retrieve (Get-AzureVM) and shutdown (Stop-AzureVM) Azure Virtual Machines.
.PARAMETER VirtualMachineLike
Accepts a string with wildcard characters, to be fed to a -like statement.
.PARAMETER AzureClassic
If $true, the script will use the classic (non-Resource Manager/RM/ARM) cmdlets for Azure Classic VMs.
.EXAMPLE
The workflow is unlikely to ever be run like this, but this gives you an idea of how to fill out the parameters when prompted by Azure Automation.
Shutdown-AzureVM.ps1 -CredentialName "Azure Automation Unattended account" -Subscription "Ashley Azure" -VirtualMachineLike "azurevm-*"
.NOTES
Author: ashley.geek.nz
Version: 2016-04-11 00:00 BST
Github: https://github.com/webash/azure-automation/
The credential stored in the asset library within Azure Automation will need the permission (Virtual Machine contributor or higher) within the subscription in order to stop VMs.
See Shutdown-AzureVMs.ps1 in the same https://github.com/webash/azure-automation/ repository to _stop_ VMs too.
.LINK
https://ashley.geek.nz/2016/03/31/using-azure-automation-to-stop-and-start-azure-iaas-virtual-machines/
#>
workflow Shutdown-AzureVMs
{
param (
[parameter(Mandatory=$true)]
[string]$CredentialName,
[parameter(Mandatory=$true)]
[string]$Subscription,
[parameter(Mandatory=$true)]
[string]$VirtualMachineLike,
[parameter(Mandatory=$false)]
[bool]$AzureClassic = $false
)
Write-Output ( [string]::Format("----- Script Start {0} -----", (Get-Date).toString() ))
if ( $AzureClassic ) {
Write-Output "Azure VM Classic cmdlets will be used for this session because -AzureClassic is true"
}
Write-Output "Using credential named $CredentialName"
$credential = Get-AutomationPSCredential -Name $CredentialName
if ( -not $AzureClassic ) {
Add-AzureRmAccount -Credential $credential
} else {
Add-AzureAccount -Credential $credential
}
Write-Output "Shutting down VMs in $subscription"
if ( -not $AzureClassic ) {
Select-AzureRmSubscription -SubscriptionName $subscription
} else {
Select-AzureSubscription -SubscriptionName $subscription
}
Write-Output "Gathering VMs with Name -like $VirtualMachineLike"
if ( -not $AzureClassic ) {
$RawVMs = Get-AzureRmVM
} else {
$RawVMs = Get-AzureVM
}
$VMs = $RawVMs | Where-Object -FilterScript { $_.Name -like $VirtualMachineLike }
if ( -not $AzureClassic ) {
$VMs | Get-AzureRmVm -Status | Get-AzureRmVm -Status | Foreach-Object { Write-Output ([string]::Format("`t{0}\{1}: {2}, {3}", $_.ResourceGroupName, $_.Name, (($_.StatusesText | convertfrom-json) | Where-Object -FilterScript { $_.code -like "PowerState*" }).code, (($_.StatusesText | convertfrom-json) | Where-Object -FilterScript { $_.code -like "PowerState*" }).displayStatus)) }
} else {
$VMs | Foreach-Object { Write-Output ([string]::Format("`tClassic\{0}: {1}, {2}", $_.Name, $_.PowerState, $_.Status)) }
}
Write-Output "Stopping pattern-matched VMs in parallel..."
ForEach -Parallel ($VM in $VMs){
Write-Output ([string]::Format("Shutting Down VM {0}...", $VM.Name))
if ( -not $AzureClassic ) {
$operationOutput = $VM | Stop-AzureRmVM -force
if ( $operationOutput.IsSuccessStatusCode ) {
Write-Output ([string]::Format("`t{0}: {1}", $VM.Name, $operationOutput.ReasonPhrase))
} else {
Write-Error ([string]::Format("`t{0} error: {1}", $VM.Name, $operationOutput.ReasonPhrase))
}
} else {
$operationOutput = $VM | Stop-AzureVM -force
Write-Output ([string]::Format("`t{0}: {1}", $VM.Name, $operationOutput.OperationStatus))
}
}
Write-Output "Confirming new status of VMs with Name -like $VirtualMachineLike"
if ( -not $AzureClassic ) {
$RawVMs = Get-AzureRmVM
} else {
$RawVMs = Get-AzureVM
}
$VMs = $RawVMs | Where-Object -FilterScript { $_.Name -like $VirtualMachineLike }
if ( -not $AzureClassic ) {
$VMs | Get-AzureRmVm -Status | Foreach-Object { Write-Output ([string]::Format("`t{0}\{1}: {2}, {3}", $_.ResourceGroupName, $_.Name, (($_.StatusesText | convertfrom-json) | Where-Object -FilterScript { $_.code -like "PowerState*" }).code, (($_.StatusesText | convertfrom-json) | Where-Object -FilterScript { $_.code -like "PowerState*" }).displayStatus)) }
} else {
$VMs | Foreach-Object { Write-Output ([string]::Format("`tClassic\{0}: {1}, {2}", $_.Name, $_.PowerState, $_.Status)) }
}
Write-Output ( [string]::Format("----- Script Stop {0} -----", (Get-Date).toString() ))
}