-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSimpleAzureVMStartStop.ps1
222 lines (190 loc) · 7.33 KB
/
SimpleAzureVMStartStop.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
<#
.SYNOPSIS
Script to start or stop Azure VMs with Azure Automation.
.DESCRIPTION
This script is intended to start or stop Azure Virtual Machines in a simple way in Azure Automation.
The script uses Azure Automation Managed Identity and the modern ("Az") Azure PowerShell Module.
Both system-assigned and user-assigned Managed Identites are supported.
Requirements:
Give the Azure Automation Managed Identity necessary rights to Start/Stop VMs in the Resource Group.
You can create a custom role for this purpose with the following permissions:
- Microsoft.Compute/virtualMachines/deallocate/action
- Microsoft.Compute/virtualMachines/start/action
- Microsoft.Compute/virtualMachines/read
.NOTES
Version: 1.3.0
Author: Andreas Dieckmann
Creation Date: 2023-09-21
Last update: 2024-03-20
GitHub: https://github.com/diecknet/Simple-Azure-VM-Start-Stop
Blog: https://diecknet.de
License: MIT License
Copyright (c) 2024 Andreas Dieckmann and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
.LINK
https://diecknet.de/
.LINK
https://github.com/diecknet/Simple-Azure-VM-Start-Stop
.INPUTS
None
.OUTPUTS
String to determine result of the script
.PARAMETER UserAssignedIdentityClientId
Specify the Managed Identity Client ID if applicable.
.PARAMETER VMName
Specify the name of the Virtual Machine, or use the asterisk symbol "*" to affect all VMs in the resource group.
.PARAMETER ResourceGroupName
Specifies the name of the resource group containing the VM(s).
.PARAMETER AzureSubscriptionID
Optionally specify Azure Subscription ID.
.PARAMETER Action
Specify desired Action, allowed values "Start" or "Stop".
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, HelpMessage = "Specify the Managed Identity Client ID if applicable.")]
[string]
$UserAssignedIdentityClientId,
[Parameter(Mandatory = $true, HelpMessage = "Specify the VM name or '*' for all VMs in the resource group.")]
[string]
$VMName,
[Parameter(Mandatory = $true, HelpMessage = "Specify the name of the resource group containing the VM(s).")]
[string]
$ResourceGroupName,
[Parameter(Mandatory = $false, HelpMessage = "Optionally specify the Azure Subscription ID.")]
[string]
$AzureSubscriptionID,
[Parameter(Mandatory = $true, HelpMessage = "Specify 'Start' or 'Stop' to control the VM(s).")]
[ValidateSet("Start", "Stop")]
[string]
$Action
)
Write-Output "Script started at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
# temporarily hide Warning message (see Issue #2 in Github)
$WarningPreference = "SilentlyContinue"
# explicitly load the required PowerShell Az modules
Import-Module Az.Accounts,Az.Compute
# re-set WarningPreference to show Warning Messages
$WarningPreference = "Continue"
$errorCount = 0
# connect to Azure, suppress output
try {
if($UserAssignedIdentityClientId) {
Write-Output "Trying to connect to Azure with a User assigned Identity, with the Client ID $UserAssignedIdentityClientId..."
$null = Connect-AzAccount -Identity -AccountId $UserAssignedIdentityClientId
}
else {
Write-Output "Trying to connect to Azure with a system assigned Identity..."
$null = Connect-AzAccount -Identity
}
}
catch {
$ErrorMessage = "Error connecting to Azure: " + $_.Exception.message
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
# select Azure subscription by ID if specified, suppress output
if ($AzureSubscriptionID) {
try {
$null = Select-AzSubscription -SubscriptionID $AzureSubscriptionID
}
catch {
$ErrorMessage = "Error selecting Azure Subscription ($AzureSubscriptionID): " + $_.Exception.message
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
}
# check if we are in an Azure Context
try {
$AzContext = Get-AzContext
}
catch {
$ErrorMessage = "Error while trying to retrieve the Azure Context: " + $_.Exception.message
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
if ([string]::IsNullOrEmpty($AzContext.Subscription)) {
$ErrorMessage = "Error. Didn't find any Azure Context. Have you assigned the permissions according to 'CustomRoleDefinition.json' to the Managed Identity?"
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
if ($VMName -eq "*") {
try {
# if "*" was given as the VMName, get all VMs in the resource group
$VMs = Get-AzVM -ResourceGroupName $ResourceGroupName -ErrorAction Stop
}
catch {
$ErrorMessage = "Error getting VMs from resource group ($ResourceGroupName): " + $_.Exception.message
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
}
else {
try {
# get only the specified VM
$VMs = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -ErrorAction Stop
}
catch {
$ErrorMessage = "Error getting VM ($VMName) from resource group ($ResourceGroupName): " + $_.Exception.message
Write-Error $ErrorMessage
throw $ErrorMessage
exit
}
}
# Loop through all specified VMs (if more than one). The loop only executes once if only one VM is specified.
foreach ($VM in $VMs) {
switch ($Action) {
"Start" {
# Start the VM
try {
Write-Output "Starting VM $($VM.Name)..."
$null = $VM | Start-AzVM -ErrorAction Stop -NoWait
}
catch {
$ErrorMessage = $_.Exception.message
Write-Error "Error starting the VM $($VM.Name): " + $ErrorMessage
# increase error count
$errorCount++
Break
}
}
"Stop" {
# Stop the VM
try {
Write-Output "Stopping VM $($VM.Name)..."
$null = $VM | Stop-AzVM -ErrorAction Stop -Force -NoWait
}
catch {
$ErrorMessage = $_.Exception.message
Write-Error "Error stopping the VM $($VM.Name): " + $ErrorMessage
# increase error count
$errorCount++
Break
}
}
}
}
$endOfScriptText = "Script ended at $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
if ($errorCount -gt 0) {
throw "Errors occured: $errorCount `r`n$endofScriptText"
}
Write-Output $endOfScriptText