-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStart-AzureVMs.ps1
165 lines (134 loc) · 4.41 KB
/
Start-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
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
<#
.SYNOPSIS
The script can be used to start Azure VMs, for example when they need to update using Update Management with Automation Account.
.DESCRIPTION
The script does the following:
* Starting the Azure VM when status is not "running"
Required Powershell modules:
'Az.Compute'
'Az.Resources'
'Az.Automation'
.PARAMETER SubscriptionId
Subscription ID of where the Session Hosts are hosted
.PARAMETER SkipTag
The name of the tag, which will exclude the VM from scaling. The default value is SkipAutoShutdown
.PARAMETER TimeDifference
The time diference with UTC (e.g. +2:00)
.NOTES
Version: 1.0
Author: Siebren Mossel
Creation Date: 15/02/2023
Purpose/Change: Initial script development
#>
param(
[Parameter(mandatory = $true)]
[string]$SubscriptionId,
[Parameter(mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(mandatory = $false)]
[string]$SkipTag = "SkipStart",
[Parameter(mandatory = $false)]
[string]$TimeDifference = "+2:00"
)
[array]$RequiredModules = @(
'Az.Compute'
'Az.Resources'
'Az.Automation'
)
[string[]]$TimeDiffHrsMin = "$($TimeDifference):0".Split(':')
#Functions
function Write-Log {
# Note: this is required to support param such as ErrorAction
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Message,
[switch]$Err,
[switch]$Warn
)
[string]$MessageTimeStamp = (Get-LocalDateTime).ToString('yyyy-MM-dd HH:mm:ss')
$Message = "[$($MyInvocation.ScriptLineNumber)] $Message"
[string]$WriteMessage = "$MessageTimeStamp $Message"
if ($Err) {
Write-Error $WriteMessage
$Message = "ERROR: $Message"
}
elseif ($Warn) {
Write-Warning $WriteMessage
$Message = "WARN: $Message"
}
else {
Write-Output $WriteMessage
}
}
# Function to return local time converted from UTC
function Get-LocalDateTime {
return (Get-Date).ToUniversalTime().AddHours($TimeDiffHrsMin[0]).AddMinutes($TimeDiffHrsMin[1])
}
# Authenticating
try
{
Write-log "Logging in to Azure..."
$connecting = Connect-AzAccount -identity
}
catch {
Write-Error -Message $_.Exception
Write-log "Unable to sign in, terminating script.."
throw $_.Exception
}
#starting script
Write-Log 'Starting script for starting Azure VMs'
Write-Log 'Checking if required modules are installed in the Automation Account'
# Checking if required modules are present
foreach ($ModuleName in $RequiredModules) {
if (Get-Module -ListAvailable -Name $ModuleName) {
Write-Log "$($ModuleName) is present"
}
else {
Write-Log "$($ModuleName) is not present. Make sure to import the required modules in the Automation Account. Check the desription"
#throw
}
}
#Getting Azure VMs
Write-Log 'Getting all Azure VMs'
$AzureVMs = Get-AzVM -ResourceGroupName $ResourceGroupName
if (!$AzureVMs) {
Write-Log "There are no Azure Vms in the ResourceGroup $ResourceGroupName."
Write-Log 'End'
return
}
#Evaluate eacht session hosts
foreach ($vm in $AzureVMs) {
$vmName = $vm.Name
#Gathering information about the running state
$VMStatus = (Get-AzVM -ResourceGroupName $ResourceGroupName -Name $vmName -Status).Statuses[1].Code
#Gathering information about tags
$VMSkip = (Get-AzVm -ResourceGroupName $ResourceGroupName -Name $vmName).Tags.Keys
# If VM is Deallocated we can skip
if($VMStatus -eq 'PowerState/deallocated'){
Write-Log "$vmName is in a deallocated state, starting VM"
$StartVM = Start-AzVM -Name $vmName -ResourceGroupName $ResourceGroupName
Write-Log "Starting $vmName ended with status: $($StartVM.Status)"
}
# If VM has skiptag we can skip
if ($VMSkip -contains $SkipTag) {
Write-Log "VM $vmName contains the skip tag and will be ignored"
continue
}
# If VM is stopped, deallocate VM
if ($VMStatus -eq 'PowerState/stopped'){
Write-Log "$vmName is stopped, starting VM"
$StartVM = Start-AzVM -Name $vmName -ResourceGroupName $ResourceGroupName
Write-Log "Starting $vmName ended with status: $($StartVM.Status)"
}
#for running vms
if($VMStatus -eq 'PowerState/running'){
Write-Log "$vmName is already running"
continue
}
}
Write-Log 'All VMs are processed'
Write-Log 'Disconnecting AZ Session'
#disconnect
$DisconnectInfo = Disconnect-AzAccount
Write-Log 'End'