-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoShutdownSchedule.ps1
228 lines (193 loc) · 8.02 KB
/
AutoShutdownSchedule.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
223
224
225
226
227
228
workflow AutoShutdownSchedule
{
# Settings
$TimeZone = "W. Europe Standard Time"
$TAGName = "AutoShutdownSchedule"
# Get Current Time
$currentTime = (Get-Date).ToUniversalTime()
$CurrentTime = Get-CurrentTime -TimeZone $TimeZone
"Runbook started"
"Current UTC/GMT time [$($currentTime.ToString("dddd, yyyy MMM dd HH:mm:ss"))]"
"Current $TimeZone time [$($currentTime.ToString("dddd, yyyy MMM dd HH:mm:ss"))]"
# Connecting to Azure
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
# Running Logic
"Getting ARM based virtual machines with AutoShutdownSchedule tag"
$VirtualMachines = Get-AzureRmVM | Where {$_.Tags.AutoShutdownSchedule}
if(!$VirtualMachines){
Throw "Could not fing any ARM based VMs with AutoShutdownSchedule tag"
}
$startVMs = @()
$stopVMs = @()
ForEach ($VM in $VirtualMachines){
$Tag = ($VM.TagsText | ConvertFrom-Json)
"[$($VM.Name)] $TAGName = $($tag.$TAGName)"
# Get the shutdown time ranges definition tag and extract the value
$shutdownTag = $tag.$TAGName
$shutdownTimeRangesDefinition = $shutdownTag
# Parse the ranges in the Tag value. Expects a string of comma-separated time ranges, or a single time range
$timeRangeList = $shutdownTimeRangesDefinition -split "," | foreach {$_.Trim()}
$checkScheduleEntry = 'not matched'
$matchedEntry = $null
foreach($entry in $timeRangeList){
if($checkScheduleEntry -eq 'not matched'){
$checkScheduleEntry = CheckScheduleEntry -TimeRange $entry -currentTime $currentTime
"[$($VM.Name)] $entry $checkScheduleEntry against $currentTime"
$matchedEntry = $entry
}
}
# Record desired state for virtual machine based on result. If schedule is matched, shut down the VM if it is running. Otherwise start the VM if stopped.
if($checkScheduleEntry -eq 'matched')
{
"[$($VM.Name)] Current time falls within the range [$matchedEntry]"
$targetState = "PowerState/Deallocated"
}
else
{
"[$($VM.Name)] Current time '$(Get-Date($currenttime) -format "dddd HH:mm")' is outside of all shutdown schedule ranges"
$targetState = "PowerState/running"
}
$VMPowerState = (Get-AzureRmVm -Status $VM.ResourceGroupName $VM.Name).Statuses.Code[1]
if ($VMPowerState -eq $targetState){
"[$($VM.Name)] PowerState '$VMPowerState' equals target state - Do Nothing"
}
else{
if($targetState -eq "PowerState/running"){
$startVMs += $VM
"[$($VM.Name)] PowerState '$VMPowerState' does not match targetState '$targetState' - Start virtual machine"
}
if ($targetState -eq "PowerState/Deallocated"){
$stopVMs += $VM
"[$($VM.Name)] PowerState '$VMPowerState' does not match targetState '$targetState' - Stop virtual machine"
}
}
}
If ($startVMs){
ForEach -Parallel ($VM in $startVMs){
"[ACTION] Starting virtual machines [$($VM.Name)] in resource group [$($VM.ResourceGroupName)]"
$VM | Start-AzureRmVm -verbose
""
}
}
else{
"[INFO] No virtual machine to start"
}
If ($stopVMs){
ForEach -Parallel ($VM in $stopVMs){
"[ACTION] Stopping virtual machines [$($VM.Name)] in resource group [$($VM.ResourceGroupName)]"
$VM | Stop-AzureRmVm -Force -verbose
""
}
}
else{
"[INFO] No virtual machine to stop"
}
""
"Runbook completed"
# Functions
# Function - Get Current Time
Function Get-CurrentTime{
Param(
[String]$TimeZone = $TimeZone
)
$TimeZones = $null
$timeZones = [System.TimeZoneInfo]::GetSystemTimeZones() | Where-Object StandardName -Match $TimeZone
$timeZones = $timeZones | Add-Member -MemberType ScriptProperty -Name "CurrentTime" -Value { [TimeZoneInfo]::ConvertTime([DateTIme]::Now, $this) } -PassThru -Force
$timeZones = $timeZones | Add-Member -MemberType ScriptProperty -Name "IsDayLightSavingTime" -Value { $this.IsDaylightSavingTime([DateTime]::Now) } -PassThru -Force
$timeZones | Add-Member -MemberType ScriptProperty -Name "HoursApart" -Value { $this.CurrentTime - [DateTime]::Now } -PassThru -Force | out-null
Return $timeZones.CurrentTime
}
# Fnction - Check Schedule
Function CheckScheduleEntry{
Param(
$TimeRange,
$currentTime
)
try
{
# Parse as range if contains '->'
if($TimeRange -like "*->*")
{
$timeRangeComponents = $TimeRange -split "->" | foreach {$_.Trim()}
if($timeRangeComponents.Count -eq 2)
{
$rangeStart = Get-Date $timeRangeComponents[0]
$rangeEnd = Get-Date $timeRangeComponents[1]
$midnight = $currentTime.AddDays(1).Date
# Check for crossing midnight
if($rangeStart -gt $rangeEnd)
{
# If current time is between the start of range and midnight tonight, interpret start time as earlier today and end time as tomorrow
if($currentTime -ge $rangeStart -and $currentTime -lt $midnight)
{
$rangeEnd = $rangeEnd.AddDays(1)
}
# Otherwise interpret start time as yesterday and end time as today
else
{
$rangeStart = $rangeStart.AddDays(-1)
}
}
}
else
{
Write-Error "`tWARNING: Invalid time range format. Expects valid .Net DateTime-formatted start time and end time separated by '->'"
}
}
# Otherwise attempt to parse as a full day entry, e.g. 'Monday' or 'December 25'
else{
# If specified as day of week, check if today
if([System.DayOfWeek].GetEnumValues() -contains $TimeRange){
$Today = (Get-Date).DayOfWeek
if($TimeRange -eq $Today){
$parsedDay = Get-Date "00:00"
}
else{
# Skip detected day of week that isn't today
}
}
# Otherwise attempt to parse as a date, e.g. 'December 25'
else{
$parsedDay = Get-Date $TimeRange
}
if($parsedDay -ne $null){
$rangeStart = $parsedDay # Defaults to midnight
$rangeEnd = $parsedDay.AddHours(23).AddMinutes(59).AddSeconds(59) # End of the same day
}
}
}
catch{
# Record any errors and return false by default
Write-Error "`tWARNING: Exception encountered while parsing time range. Details: $($_.Exception.Message). Check the syntax of entry, e.g. '<StartTime> -> <EndTime>', or days/dates like 'Sunday' and 'December 25'"
return $false
}
# Check if current time falls within range
if($currentTime -ge $rangeStart -and $currentTime -le $rangeEnd){
return 'matched'
}
else{
return 'not matched'
}
}
}