forked from rgel/Azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apply-AzVmPowerStatePolicy.ps1
176 lines (165 loc) · 5.58 KB
/
Apply-AzVmPowerStatePolicy.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
<#
.SYNOPSIS
Start/Stop Azure VM in parallel on schedule based on two VM Tags (PowerOn/PowerOff).
.DESCRIPTION
This Azure Automation PowerShell Workflow type Runbook Start/Stop Azure VM in parallel on schedule based on two VM Tags (PowerOn/PowerOff).
.NOTES
Author :: Roman Gelman @rgelman75
Dependency :: AzureRm PowerShell module
Version 1.0 :: 19-Jun-2016 :: Release
Version 1.1 :: 20-Jun-2016 :: Error handling added
Version 1.2 :: 07-Jul-2016 :: -WhatIf parameter type changed from [switch] to [boolean]
Version 1.3 :: 25-Jun-2017 :: Flags '11:11:11' & '22:22:22' added to disable PowerOn/Off schedule
.LINK
https://ps1code.com/category/powershell/azure/
#>
Workflow Apply-AzVmPowerStatePolicy
{
Param (
[Parameter(Mandatory, Position = 1)]
[string]$AzureCredentialAsset = 'automation'
,
[Parameter(Mandatory, Position = 2)]
[string]$AzureSubscription
,
[Parameter(Mandatory, Position = 3)]
[string]$AzureResourceGroup
,
#[System.TimeZoneInfo]::GetSystemTimeZones() |ft -au
[Parameter(Mandatory = $false, Position = 4)]
[string]$AzureVmTimeZone = 'New Zealand Standard Time'
,
[Parameter(Mandatory = $false, Position = 5)]
[boolean]$WhatIf = $true
)
$ErrorActionPreference = 'Stop'
$azCredential = Get-AutomationPSCredential -Name $AzureCredentialAsset
$null = Login-AzureRmAccount -Credential $azCredential
$null = Set-AzureRmContext -SubscriptionName $AzureSubscription
$AzVms = Get-AzureRmVm -ResourceGroupName $AzureResourceGroup |
select Name, Tags, @{ N = 'PowerState'; E = { (Get-AzureRmVM -Name $_.Name -ResourceGroupName $_.ResourceGroupName -Status |
select -expand Statuses | ? { $_.Code -match 'PowerState/' } |
select @{ N = 'PowerState'; E = { $_.Code.Split('/')[1] } }).PowerState } } | sort Name
Foreach -Parallel ($AzVm in $AzVms)
{
Try
{
### Running VM ###
if ($AzVm.PowerState -eq 'running')
{
### Flag to NOT PowerOff ###
if ($AzVm.Tags.PowerOff -ne '22:22:22')
{
$azTime = [datetime]::Now
$TimeShort = $azTime.ToString('HH:mm')
$TimeVm = [System.TimeZoneInfo]::ConvertTimeFromUtc($TimeShort, [System.TimeZoneInfo]::FindSystemTimeZoneById($AzureVmTimeZone))
$TimeVmTime = $TimeVM.TimeOfDay
$powerOn = [datetime]$AzVm.Tags.PowerOn
$powerOnTime = $powerOn.TimeOfDay
$powerOff = [datetime]$AzVm.Tags.PowerOff
$powerOffTime = $powerOff.TimeOfDay
### 00:00---On+++Off---00:00 ###
if ($powerOnTime -lt $powerOffTime)
{
if ($TimeVmTime -gt $powerOffTime -or $TimeVmTime -lt $powerOnTime)
{
if ($WhatIf) { $Status = 'Simulation' }
else
{
$Status = (Stop-AzureRmVm -Name $AzVm.Name -ResourceGroupName $AzureResourceGroup -Force).StatusCode
}
$Execution = 'Stopped'
}
else { $Execution = 'NotRequired' }
### 00:00+++Off---On+++00:00 ###
}
else
{
if ($TimeVmTime -gt $powerOffTime -and $TimeVmTime -lt $powerOnTime)
{
if ($WhatIf) { $Status = 'Simulation' }
else
{
$Status = (Stop-AzureRmVm -Name $AzVm.Name -ResourceGroupName $AzureResourceGroup -Force).StatusCode
}
$Execution = 'Stopped'
}
else { $Execution = 'NotRequired' }
}
}
}
### Not running VM (stopped/deallocated/suspended etc.) ###
else
{
### Flag to NOT PowerOn ###
if ($AzVm.Tags.PowerOn -ne '11:11:11')
{
$azTime = [datetime]::Now
$TimeShort = $azTime.ToString('HH:mm')
$TimeVm = [System.TimeZoneInfo]::ConvertTimeFromUtc($TimeShort, [System.TimeZoneInfo]::FindSystemTimeZoneById($AzureVmTimeZone))
$TimeVmTime = $TimeVM.TimeOfDay
$powerOn = [datetime]$AzVm.Tags.PowerOn
$powerOnTime = $powerOn.TimeOfDay
$powerOff = [datetime]$AzVm.Tags.PowerOff
$powerOffTime = $powerOff.TimeOfDay
### 00:00---On+++Off---00:00 ###
if ($powerOnTime -lt $powerOffTime)
{
if ($TimeVmTime -gt $powerOnTime -and $TimeVmTime -lt $powerOffTime)
{
if ($WhatIf) { $Status = 'Simulation' }
else
{
$Status = (Start-AzureRmVm -Name $AzVm.Name -ResourceGroupName $AzureResourceGroup).StatusCode
}
$Execution = 'Started'
}
else { $Execution = 'NotRequired' }
### 00:00+++Off---On+++00:00 ###
}
else
{
if ($TimeVmTime -gt $powerOnTime -or $TimeVmTime -lt $powerOffTime)
{
if ($WhatIf) { $Status = 'Simulation' }
else
{
$Status = (Start-AzureRmVm -Name $AzVm.Name -ResourceGroupName $AzureResourceGroup).StatusCode
}
$Execution = 'Stopped'
}
else { $Execution = 'NotRequired' }
}
}
}
$Prop = [ordered]@{
AzureVM = $AzVm.Name
ResourceGroup = $AzureResourceGroup
PowerState = (Get-Culture).TextInfo.ToTitleCase($AzVm.PowerState)
PowerOn = $AzVm.Tags.PowerOn
PowerOff = $AzVm.Tags.PowerOff
StateChange = $Execution
StatusCode = $Status
TimeStamp = $TimeVm
}
}
Catch
{
$Prop = [ordered]@{
AzureVM = $AzVm.Name
ResourceGroup = $AzureResourceGroup
PowerState = (Get-Culture).TextInfo.ToTitleCase($AzVm.PowerState)
PowerOn = $AzVm.Tags.PowerOn
PowerOff = $AzVm.Tags.PowerOff
StateChange = 'Unknown'
StatusCode = 'Error'
TimeStamp = $TimeVm
}
}
Finally
{
$Obj = New-Object PSObject -Property $Prop
Write-Output -InputObject $Obj
}
}
} #End Workflow Apply-AzVmPowerStatePolicy