-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ScheduleWrapper
173 lines (147 loc) · 5.62 KB
/
Get-ScheduleWrapper
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
param(
$tableName = 'AZTable' ,
$storageAccount = 'StorageAccount',
$Sub = 'SubName'
)
$runbook = 'ScheduledStartStop_Parent'
#Authentrication
$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
}
}
$sAccount = Get-AzureRmStorageAccount | ?{ $_.StorageAccountName -eq $storageAccount }
if ( !$sAccount )
{
throw "Storage account [$storageAccount] is not present in current context."
}
$ctx = $sAccount.Context
$table = Get-AzureStorageTable -Context $ctx -Name $tableName -ErrorAction SilentlyContinue
if ( !$table )
{
throw "Storage table [$tableName] does not exist in account [$storageAccount]."
}
$rows = Get-AzureStorageTableRowAll -table $table
$schedules = $rows
$now = Get-Date
ForEach ($row in $rows)
{
If ($row.Active -eq 'true' -and $row.rowkey -eq $Sub)
{
$RG = $row.PartitionKey
$eVm = $row.ExcludeVM
if ($eVM -ne "")
{
$ExcludeStartVM += $eVM +','
$ExcludeStopVM = $ExcludeStartVM
}
$Days = $row.Days -split ','
$start = $row.UTCstart
$stop = $row.UTCstop
$ScheduleKey = $row.Schedules
If ($row.Restart -eq $true)
{
$offHours = ($now.DayOfWeek -notin $Days)
$offHours = $offHours -or ($now.TimeOfDay -ge $start)
$offHours = $offHours -or ($now.TimeOfDay -le $stop)
write-output " Off Hours Restart [$RG] [$offHours]"
If ($offHours -eq "false")
{
$RestartRG += $RG +','
}
}
elseif (!($ScheduleKey))
{
$offHours = ($now.DayOfWeek -notin $Days)
$offHours = $offHours -or ($now.TimeOfDay -le $Start)
$offHours = $offHours -or ($now.TimeOfDay -ge $Stop)
write-output " Off Hours [$RG] [$offHours]"
If ($offHours -eq "True")
{
$StopRG += $RG +','
}
else
{
$StartRG += $RG +','
}
}
else
{
foreach ($schedule in $schedules)
{
if ($schedule.active -eq 'true' -and $schedule.rowkey -match $ScheduleKey)
{
$VMsets = $schedule.VMsets
$Days = $schedule.Days -split ','
$start = $schedule.UTCstart
$stop = $schedule.UTCstop
$offHours = ($now.DayOfWeek -notin $Days)
$offHours = $offHours -or ($now.TimeOfDay -le $Start)
$offHours = $offHours -or ($now.TimeOfDay -ge $Stop)
write-output " Off Hours [$ScheduleKey]/[$RG] [$offHours] for [$VMsets]"
If ($offHours -eq "True")
{
$ScheduleStartVM += $VMsets +','
$StopScheduleRG = $RG
}
else
{
$ScheduleStopVM += $VMsets +','
$StartScheduleRG = $RG
}
}
}
if ($StartScheduleRG -ne $null)
{
$StartRG = $StartScheduleRG+','+$StartRG
}
if ($StopScheduleRG -ne $null)
{
$StopRG = $StopScheduleRG+','+$StopRG
}
}
}
}
$ExcludeStartVM += $ScheduleStartVM
$ExcludeStopVM += $ScheduleStopVM
$StopRG = $StopRG -replace ".$"
$StartRG = $StartRG -replace ".$"
$RestartRG = $RestartRG -replace ".$"
$ExcludeStartVM = $ExcludeStartVM -replace ".$"
$ExcludeStopVM = $ExcludeStopVM -replace ".$"
if (!($StartRG)) { $StartRG = 'none'}
if (!($StopRG)) { $StopRG = 'none'}
if (!($RestartRG)) { $RestartRG = 'none'}
$StartParam = @{"Action"="Start"}
$StopParam = @{"Action"="Stop"}
$RestartParam = @{"Action"="Restart"}
$automationAccountName = Get-AutomationVariable -Name 'Internal_AutomationAccountName'
$automationRG = Get-AutomationVariable -Name 'Internal_ResourceGroupName'
# Set Automation Account Global Variables
Set-AutomationVariable –Name 'External_ExcludeStartVMNames' –Value $excludeStartVM
Set-AutomationVariable –Name 'External_ExcludeStopVMNames' -Value $excludeStopVM
Set-AutomationVariable –Name 'External_Restart_ResourceGroupNames' –Value $RestartRG
Set-AutomationVariable –Name 'External_Start_ResourceGroupNames' –Value $StartRG
Set-AutomationVariable –Name 'External_Stop_ResourceGroupNames' –Value $StopRG
# Start StartStopVm_parent runbbok for Stop/Start actions
If ($StartRG -ne 'none') {Start-AzureRmAutomationRunbook -automationAccountName $automationAccountName -name $runbook -ResourceGroupName $automationRG -Parameters $StartParam}
If ($StopRG -ne 'none') {Start-AzureRmAutomationRunbook -automationAccountName $automationAccountName -name $runbook -ResourceGroupName $automationRG -Parameters $StopParam}
If ($RestartRG -ne 'none') {Start-AzureRmAutomationRunbook -automationAccountName $automationAccountName -name $runbook -ResourceGroupName $automationRG -Parameters $RestartParam}