-
Notifications
You must be signed in to change notification settings - Fork 0
/
createazurelogicapp.ps1
258 lines (216 loc) · 10.1 KB
/
createazurelogicapp.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<#
.SYNOPSIS
This is a sample script for to deploy the required resources for to schedule basic scale in Microsoft Azure.
.DESCRIPTION
This sample script will create the scale script execution trigger required resources in Microsoft Azure. Resources are azure logic app for each hostpool.
Run this PowerShell script in adminstrator mode
This script depends on Az PowerShell module. To install Az module execute the following command. Use "-AllowClobber" parameter if you have more than one version of PowerShell modules installed.
PS C:\>Install-Module Az -AllowClobber
.PARAMETER TenantGroupName
Optional
Provide the name of the tenant group in the Windows Virtual Desktop deployment.
.PARAMETER TenantName
Required
Provide the name of the tenant in the Windows Virtual Desktop deployment.
.PARAMETER HostpoolName
Required
Provide the name of the WVD Host Pool.
.PARAMETER AutomationAccountName
Required
Provide the name of the name of the automation account which is published basic scale script.
.PARAMETER RecurrenceInterval
Required
Provide the RecurrenceInterval. Scheduler job will run recurrenceInterval basis, so provide recurrence in minutes.
.PARAMETER AADTenantId
Required
Provide Tenant ID of Azure Active Directory.
.PARAMETER SubscriptionId
Required
Provide Subscription Id of the Azure.
.PARAMETER BeginPeakTime
Required
Provide begin of the peak usage time.
.PARAMETER EndPeakTime
Required
Provide end of the peak usage time.
.PARAMETER TimeDifference
Required
Provide the Time difference between local time and UTC, in hours(Example: India Standard Time is +5:30).
.PARAMETER SessionThresholdPerCPU
Required
Provide the Maximum number of sessions per CPU threshold used to determine when a new RDSH server needs to be started.
.PARAMETER MinimumNumberOfRDSH
Required
Provide the Minimum number of host pool VMs to keep running during off-peak usage time.
.PARAMETER MaintenanceTagName
Required
Provide the name of the MaintenanceTagName.
.PARAMETER LimitSecondsToForceLogOffUser
Required
Provide the number of seconds to wait before forcing users to logoff. If 0, don't force users to logoff.
.PARAMETER Location
Required
Provide the name of the Location to create azure resources.
.PARAMETER LogOffMessageTitle
Required
Provide the Message title sent to a user before forcing logoff.
.PARAMETER LogOffMessageBody
Required
Provide the Message body to send to a user before forcing logoff.
.PARAMETER LogAnalyticsWorkspaceId
Required
Provide the log anayltic workspace id.
.PARAMETER LogAnalyticsPrimaryKey
Required
Provide the log anayltic workspace primarykey.
.PARAMETER ResourcegroupName
Required
Provide the name of the resouce gorup name for to create logic app.
.PARAMETER ConnectionAssetName
Required
Provide name of the AzureRunAccount which is created manually from azure portal.
.PARAMETER WebhookURI
Required
Provide URI of the azure automation account webhook
Example: .\createazurelogicapp.ps1 -AADTenantID "Your Azure TenantID" -SubscriptionID "Your Azure SubscriptionID" -TenantGroupName "Name of the WVD Tenant Group Name" `
-TenantName "Name of the WVD Tenant Name" -HostPoolName "Name of the HostPoolName" -PeakLoadBalancingType "Load balancing type in Peak hours" -MaintenanceTagName "Name of the Tag Name" -RecurrenceInterval "Repeat job every and select the appropriate period of time in minutes (Ex. 15)" `
-BeginPeakTime "9:00" -EndPeakTime "18:00" -TimeDifference "+5:30" -SessionThresholdPerCPU 6 -MinimumNumberOfRDSH 2 -LimitSecondsToForceLogOffUser 20 –LogOffMessageTitle "System Under Maintenance" -LogOffMessageBody "Please save your work and logoff!" `
–Location "Central US" -LogAnalyticsWorkspaceId "log analytic workspace id" -LogAnalyticsPrimaryKey "log analytic workspace primary key" -ResourcegroupName "Name of the resoure group" -ConnectionAssetName "Name of the azure automation account connection" -WebhookURI "URI of the Azure automation account Webhook" -AutomationAccountName "Name of the automation account which is basic scale script published"
#>
param(
[Parameter(mandatory = $True)]
[string]$HostpoolName,
[Parameter(mandatory = $True)]
[string]$AutomationAccountName,
[Parameter(mandatory = $True)]
[string]$WebhookURI,
[Parameter(mandatory = $True)]
[int]$RecurrenceInterval,
[Parameter(mandatory = $True)]
[string]$AADTenantId,
[Parameter(mandatory = $True)]
[string]$SubscriptionId,
[Parameter(mandatory = $True)]
$BeginPeakTime,
[Parameter(mandatory = $True)]
$EndPeakTime,
[Parameter(mandatory = $True)]
$TimeDifference,
[Parameter(mandatory = $True)]
[int]$SessionThresholdPerCPU,
[Parameter(mandatory = $True)]
[int]$MinimumNumberOfRDSH,
[Parameter(mandatory = $True)]
[string]$MaintenanceTagName,
[Parameter(mandatory = $True)]
[int]$LimitSecondsToForceLogOffUser,
[Parameter(mandatory = $True)]
[string]$LogAnalyticsWorkspaceId,
[Parameter(mandatory = $True)]
[string]$LogAnalyticsPrimaryKey,
[Parameter(mandatory = $True)]
[string]$ConnectionAssetName,
[Parameter(mandatory = $True)]
[string]$Location,
[Parameter(mandatory = $True)]
[string]$ResourcegroupName,
[Parameter(mandatory = $True)]
[string]$LogOffMessageTitle,
[Parameter(mandatory = $True)]
[string]$LogOffMessageBody
)
#Initializing variables
#$RDBrokerURL = "https://rdbroker.wvd.microsoft.com"
$ScriptRepoLocation = "https://raw.githubusercontent.com/hemanth8010/WVD-Scaling-Script-Spring2020/master"
# Setting ErrorActionPreference to stop script execution when error occurs
$ErrorActionPreference = "Stop"
[System.Collections.Generic.List[System.Object]]$HostpoolNames = $HostpoolName.Split(",")
# Set the ExecutionPolicy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force -Confirm:$false
# Import Az and AzureAD modules
Import-Module Az.LogicApp
Import-Module Az.Resources
Import-Module Az.Accounts
# Get the context
$Context = Get-AzContext
if ($Context -eq $null)
{
Write-Error "Please authenticate to Azure using Login-AzAccount cmdlet and then run this script"
exit
}
<##Get the WVD context
$WVDContext = Get-RdsContext -DeploymentUrl $RDBrokerURL
if ($Context -eq $null)
{
Write-Error "Please authenticate to WVD using Add-RDSAccount -DeploymentURL 'https://rdbroker.wvd.microsoft.com' cmdlet and then run this script"
exit
}
#>
# Select the subscription
$Subscription = Select-azSubscription -SubscriptionId $SubscriptionId
Set-AzContext -SubscriptionObject $Subscription.ExtendedProperties
# Get the Role Assignment of the authenticated user
$RoleAssignment = (Get-AzRoleAssignment -SignInName $Context.Account)
if ($RoleAssignment.RoleDefinitionName -eq "Owner" -or $RoleAssignment.RoleDefinitionName -eq "Contributor")
{
# Check if the automation account exist in your Azure subscription
$CheckRG = Get-AzResourceGroup -Name $ResourcegroupName -Location $Location -ErrorAction SilentlyContinue
if (!$CheckRG) {
Write-Output "The specified resourcegroup does not exist, creating the resourcegroup $ResourcegroupName"
New-AzResourceGroup -Name $ResourcegroupName -Location $Location -Force
Write-Output "ResourceGroup $ResourcegroupName created suceessfully"
}
#Creating Azure logic app to schedule job
foreach ($HPName in $HostpoolNames) {
# Check if the hostpool load balancer type is persistent.
$HostPoolInfo = Get-AzWvdHostPool -ResourcegroupName $ResourcegroupName -Name $HPName
if ($HostpoolInfo.LoadBalancerType -eq "Persistent") {
Write-Output "$HPName hostpool configured with Persistent Load balancer.So scale script doesn't apply for this load balancertype.Scale script will execute only with these load balancer types BreadthFirst, DepthFirst. Please remove the from 'HostpoolName' input and try again"
exit
}
$SessionHostsList = Get-AzWvdSessionHost -ResourcegroupName $ResourcegroupName -HostPoolName $HPName
$SessionHostCount = ($SessionHostsList).Count
#Check if the hostpool have session hosts and compare count with minimum number of rdsh value
if ($SessionHostsList -eq $Null) {
Write-Output "Hostpool '$HPName' doesn't have session hosts. Deployment Script will skip the basic scale script configuration for this hostpool."
$RmHostpoolnames += $HPName
}
elseif ($SessionHostCount -le $MinimumNumberOfRDSH) {
Write-Output "Hostpool '$HPName' has less than the minimum number of session host required."
$Confirmation = Read-Host "Do you wish to continue configuring the scale script for these available session hosts? [y/n]"
if ($Confirmation -eq 'n') {
Write-Output "Configuring the scale script is skipped for this hostpool '$HPName'."
$RmHostpoolnames += $HPName
}
else { Write-Output "Configuring the scale script for the hostpool : '$HPName' and will keep the minimum required session hosts in running mode." }
}
$RequestBody = @{
"AADTenantId" = $AADTenantId;
"subscriptionid" = $subscriptionid;
"TimeDifference" = $TimeDifference;
"ResourceGroupName" = $ResourceGroupName;
"HostPoolName" = $HPName;
"MaintenanceTagName" = $MaintenanceTagName;
"LogAnalyticsWorkspaceId" = $LogAnalyticsWorkspaceId;
"LogAnalyticsPrimaryKey" = $LogAnalyticsPrimaryKey;
"ConnectionAssetName" = $ConnectionAssetName;
"BeginPeakTime" = $BeginPeakTime;
"EndPeakTime" = $EndPeakTime;
"MinimumNumberOfRDSH" = $MinimumNumberOfRDSH;
"SessionThresholdPerCPU" = $SessionThresholdPerCPU;
"LimitSecondsToForceLogOffUser" = $LimitSecondsToForceLogOffUser;
"LogOffMessageTitle" = $LogOffMessageTitle;
"AutomationAccountName" = $AutomationAccountName;
"LogOffMessageBody" = $LogOffMessageBody }
$RequestBodyJson = $RequestBody | ConvertTo-Json
$LogicAppName = ($HPName + "_" + "Autoscale" + "_" + "Scheduler").Replace(" ","")
$SchedulerDeployment = New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateUri "$ScriptRepoLocation/azureLogicAppCreation.json" -logicappname $LogicAppName -webhookURI $WebhookURI.Replace("`n","").Replace("`r","") -actionSettingsBody $RequestBodyJson -recurrenceInterval $RecurrenceInterval -Verbose
if ($SchedulerDeployment.ProvisioningState -eq "Succeeded") {
Write-Output "$HPName hostpool successfully configured with logic app scheduler"
}
}
}
else
{
Write-Output "Authenticated user should have the Owner/Contributor permissions"
}