-
Notifications
You must be signed in to change notification settings - Fork 15
/
AzureAS.AutoScheduler.ps1
122 lines (95 loc) · 3.44 KB
/
AzureAS.AutoScheduler.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
param(
[string] $resourceGroupName = "SSASAzureTest",
[string] $serverName = "rrdvsssassales",
[string] $azureProfilePath = "",
[string] $azureRunAsConnectionName = "",
[string] $configStr = "
[
{
WeekDays:[1,2,3,4,5]
,StartTime: ""08:00:00""
,StopTime: ""17:59:59""
,Sku: ""S1""
}
,
{
WeekDays:[1,2,3,4,5]
,StartTime: ""18:00:00""
,StopTime: ""23:59:59""
,Sku: ""S0""
}
,
{
WeekDays:[6, 0]
,StartTime: ""08:00:00""
,StopTime: ""23:59:59""
,Sku: ""S0""
}
]
"
)
$VerbosePreference = "Continue"
$ErrorActionPreference = "Stop"
Import-Module "AzureRM.AnalysisServices"
Write-Verbose "Logging in to Azure..."
# Load the profile from local file
if (-not [string]::IsNullOrEmpty($azureProfilePath))
{
Import-AzureRmContext -Path $azureProfilePath | Out-Null
}
# Load the profile from Azure Automation RunAS connection
elseif (-not [string]::IsNullOrEmpty($azureRunAsConnectionName))
{
$runAsConnectionProfile = Get-AutomationConnection -Name $azureRunAsConnectionName
Add-AzureRmAccount -ServicePrincipal -TenantId $runAsConnectionProfile.TenantId `
-ApplicationId $runAsConnectionProfile.ApplicationId -CertificateThumbprint $runAsConnectionProfile.CertificateThumbprint | Out-Null
}
# Interactive Login
else
{
Add-AzureRmAccount | Out-Null
}
$stateConfig = $configStr | ConvertFrom-Json
$startTime = Get-Date
$currentDayOfWeek = [Int]($startTime).DayOfWeek
# Find a match in the config
$dayObjects = $stateConfig | Where-Object {$_.WeekDays -contains $currentDayOfWeek } `
| Select-Object Sku, @{Name="StartTime"; Expression = {[datetime]::ParseExact($_.StartTime,"HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture)}}, @{Name="StopTime"; Expression = {[datetime]::ParseExact($_.StopTime,"HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture)}}
# Get the server status
$asServer = Get-AzureRmAnalysisServicesServer -ResourceGroupName $resourceGroupName -Name $serverName
Write-Verbose "Current Azure AS status: $($asServer.State)"
# If not match any day then exit
if($dayObjects -ne $null){
# Can't treat several objects for same time-frame, if there's more than one, pick first
$matchingObject = $dayObjects | Where-Object { ($startTime -ge $_.StartTime) -and ($startTime -lt $_.StopTime) } | Select-Object -First 1
if($matchingObject -ne $null)
{
Write-Verbose "Is in active hours"
# if Paused resume
if($asServer.State -eq "Paused")
{
$asServer | Resume-AzureRmAnalysisServicesServer -Verbose
}
# Change the SKU if needed
if($asServer.Sku.Name -ne $matchingObject.Sku){
Write-Verbose "Updating AAS server from $($asServer.Sku.Name) to $($matchingObject.Sku)"
#$asServer | Set-AzureRmAnalysisServicesServer -Sku $matchingObject.Sku
Set-AzureRmAnalysisServicesServer -Name $asServer.Name -ResourceGroupName $resourceGroupName -Sku $matchingObject.Sku -Verbose
}
}
else {
Write-Verbose "Is in offline hours"
if($asServer.State -eq "Succeeded")
{
$asServer | Suspend-AzureRmAnalysisServicesServer -Verbose
}
}
}
else
{
Write-Verbose "No object config for current day of week"
if($asServer.State -eq "Succeeded")
{
$asServer | Suspend-AzureRmAnalysisServicesServer -Verbose
}
}