-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetup-WVDHost.ps1
278 lines (223 loc) · 11.5 KB
/
Setup-WVDHost.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<#
.SYNOPSIS
.DESCRIPTION
.
.ROLE
#>
param(
[Parameter(mandatory = $true)]
[string]$RDBrokerURL,
[Parameter(mandatory = $true)]
[string]$definedTenantGroupName,
[Parameter(mandatory = $true)]
[string]$TenantName,
[Parameter(mandatory = $true)]
[string]$HostPoolName,
[Parameter(mandatory = $false)]
[string]$Description,
[Parameter(mandatory = $false)]
[string]$FriendlyName,
[Parameter(mandatory = $true)]
[string]$Hours,
[Parameter(mandatory = $true)]
[string]$TenantAdminUPN,
[Parameter(mandatory = $true)]
[string]$TenantAdminPassword,
[Parameter(mandatory = $false)]
[string]$isServicePrincipal = "False",
[Parameter(Mandatory = $false)]
[string]$AadTenantId
)
function Write-Log {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$Message,
[Parameter(Mandatory = $false)]
[string]$Error
)
try {
$DateTime = Get-Date -Format 'MM-dd-yy HH:mm:ss'
$Invocation = "$($MyInvocation.MyCommand.Source):$($MyInvocation.ScriptLineNumber)"
if ($Message) {
Add-Content -Value "$DateTime - $Invocation - $Message" -Path "$WVDDeployLogPath\ScriptLog.log"
}
else {
Add-Content -Value "$DateTime - $Invocation - $Error" -Path "$WVDDeployLogPath\ScriptLog.log"
}
}
catch {
Write-Error $_.Exception.Message
}
}
# Get Start Time
$startDTM = (Get-Date)
Write-Log -Message "Starting WVD Deploy on Host"
# Setting to Tls12 due to Azure web app security requirements
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$WVDDeployBasePath = "c:\WVDDeploy\"
$WVDDeployLogPath = "c:\WVDDeploy\logs"
$WVDDeployBootPath = "C:\WVDDeploy\Boot"
$WVDDeployInfraPath = "C:\WVDDeploy\Infra"
$WVDDeployFslgxPath = "C:\WVDDeploy\fslogix"
$BootURI = "https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RWrxrH"
$infraURI = "https://query.prod.cms.rt.microsoft.com/cms/api/am/binary/RWrmXv"
$fslgxURI = "https://aka.ms/fslogix_download"
# Creating a folder inside rdsh vm for agents and log files
New-Item -Path $WVDDeployLogPath -ItemType Directory -Force
New-Item -Path $WVDDeployBootPath -ItemType Directory -Force
New-Item -Path $WVDDeployInfraPath -ItemType Directory -Force
New-Item -Path $WVDDeployFslgxPath -ItemType Directory -Force
$AssetstartDTM = (Get-Date)
Write-Log -Message "Created Directory Structure Begining Setup for WVD"
Invoke-WebRequest -Uri $BootURI -OutFile "$WVDDeployBootPath\Microsoft.RDInfra.RDAgentBootLoader.Installer-x64.msi"
Write-Log -Message "Downloaded RDAgentBootLoader"
Invoke-WebRequest -Uri $infraURI -OutFile "$WVDDeployInfraPath\Microsoft.RDInfra.RDAgent.Installer-x64.msi"
Write-Log -Message "Downloaded RDInfra"
Invoke-WebRequest -Uri $fslgxURI -OutFile "$WVDDeployBasePath\FSLogix_Apps.zip"
Write-Log -Message "Downloaded FSLogix"
Expand-Archive "$WVDDeployBasePath\FSLogix_Apps.zip" -DestinationPath "$WVDDeployFslgxPath" -ErrorAction SilentlyContinue
Remove-Item "$WVDDeployBasePath\FSLogix_Apps.zip"
$AssetendDTM = (Get-Date)
Write-Log -Message "Asset Download Time: $(($AssetendDTM-$AssetstartDTM).totalseconds) seconds"
# Checking if RDInfragent is registered or not in rdsh vm
$CheckRegistry = Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RDInfraAgent" -ErrorAction SilentlyContinue
Write-Log -Message "Checking whether VM was Registered with RDInfraAgent"
if ($CheckRegistry) {
Write-Log -Message "VM was already registered with RDInfraAgent, script execution was stopped"
}
else {
Write-Log -Message "VM was not registered with RDInfraAgent, script is executing"
}
if (!$CheckRegistry) {
# Installing & Importing WVD PowerShell module
If(-not(Get-InstalledModule Microsoft.RDInfra.RDPowerShell -ErrorAction silentlycontinue)){
Install-PackageProvider NuGet -Force
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module Microsoft.RDInfra.RDPowerShell -Confirm:$False -Force
Write-Log -Message "Installed RDMI PowerShell modules successfully"
}
Import-Module -Name Microsoft.RDInfra.RDPowerShell
Write-Log -Message "Imported RDMI PowerShell modules successfully"
#Build Credential Variables
$Securepass = ConvertTo-SecureString -String $TenantAdminPassword -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($TenantAdminUPN, $Securepass)
# Getting fqdn of rdsh vm
$SessionHostName = (Get-WmiObject win32_computersystem).DNSHostName + "." + (Get-WmiObject win32_computersystem).Domain
Write-Log -Message "Getting fully qualified domain name of RDSH VM: $SessionHostName"
# Authenticating to WVD
if ($isServicePrincipal -eq "True"){
$authentication = Add-RdsAccount -DeploymentUrl $RDBrokerURL -Credential $Credentials -ServicePrincipal -TenantId $AadTenantId
} else {
$authentication = Add-RdsAccount -DeploymentUrl $RDBrokerURL -Credential $Credentials
}
$obj = $authentication | Out-String
if ($authentication) {
Write-Log -Message "RDMI Authentication successfully Done. Result: `
$obj"
}
else {
Write-Log -Error "RDMI Authentication Failed, Error: `
$obj"
}
# Set context to the appropriate tenant group
Write-Log "Running switching to the $definedTenantGroupName context"
Set-RdsContext -TenantGroupName $definedTenantGroupName
try {
$tenants = Get-RdsTenant
if( !$tenants ) {
Write-Log "No tenants exist or you do not have proper access."
}
} catch {
Write-Log -Message ""
}
# Checking if host pool exists. If not, create a new one with the given HostPoolName
$HPName = Get-RdsHostPool -TenantName $TenantName -Name $HostPoolName -ErrorAction SilentlyContinue
Write-Log -Message "Checking Hostpool exists inside the Tenant"
if ($HPName) {
Write-log -Message "Hostpool $HPName, exists inside tenant: $TenantName"
}
else {
Write-log -Message "Hostpool $HPName, does not exist inside tenant: $TenantName"
Write-log -Message "Creating $HPName"
$HPName = New-RdsHostPool -TenantName $TenantName -Name $HostPoolName -Description $Description -FriendlyName $FriendlyName
$HName = $HPName.name | Out-String -Stream
Write-Log -Message "Successfully created new Hostpool: $HName"
}
# Setting UseReverseConnect property to true
Write-Log -Message "Checking Hostpool UseResversconnect is true or false"
if ($HPName.UseReverseConnect -eq $False) {
Write-Log -Message "UseReverseConnect is false, it will be changed to true"
Set-RdsHostPool -TenantName $TenantName -Name $HostPoolName -UseReverseConnect $true
}
else {
Write-Log -Message "Hostpool UseReverseConnect already enabled as true"
}
# Creating registration token
$Registered = $null
try {
$Registered = Export-RdsRegistrationInfo -TenantName $TenantName -HostPoolName $HostPoolName
if (!$Registered) {
$Registered = New-RdsRegistrationInfo -TenantName $TenantName -HostPoolName $HostPoolName -ExpirationHours $Hours
Write-Log -Message "Created new Rds RegistrationInfo into variable 'Registered': $Registered"
} else {
Write-Log -Message "Exported Rds RegistrationInfo into variable 'Registered': $Registered"
}
} catch {
$Registered = New-RdsRegistrationInfo -TenantName $TenantName -HostPoolName $HostPoolName -ExpirationHours $Hours
Write-Log -Message "Created new Rds RegistrationInfo into variable 'Registered': $Registered"
}
#Get MSI Paths for Install
$AgentBootServiceInstaller = (dir $WVDDeployBootPath\ -Filter *.msi | Select-Object).FullName
$AgentInstaller = (dir $WVDDeployInfraPath\ -Filter *.msi | Select-Object).FullName
$RegistrationToken = $Registered.Token
#Boot Install
# Uninstalling previous versions of RDAgentBootLoader
Write-Log -Message "Uninstalling any previous versions of RDAgentBootLoader on VM"
$bootloader_uninstall_status = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x {A38EE409-424D-4A0D-B5B6-5D66F20F62A5}", "/quiet", "/qn", "/norestart", "/passive", "/l* $WVDDeployLogPath\AgentBootLoaderInstall.txt" -Wait -Passthru
$sts = $bootloader_uninstall_status.ExitCode
# Installing RDAgentBootLoader
Write-Log -Message "Starting install of $AgentBootServiceInstaller"
$bootloader_deploy_status = Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $AgentBootServiceInstaller", "/quiet", "/qn", "/norestart", "/passive", "/l* $WVDDeployLogPathAgentBootLoaderInstall.txt" -Wait -Passthru
$sts = $bootloader_deploy_status.ExitCode
Write-Log -Message "Installing RDAgentBootLoader on VM Complete. Exit code=$sts"
#Infra Install
# Uninstalling previous versions of RDInfraAgent
Write-Log -Message "Uninstalling any previous versions of RD Infra Agent on VM"
$legacy_agent_uninstall_status = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x {5389488F-551D-4965-9383-E91F27A9F217}", "/quiet", "/qn", "/norestart", "/passive", "/l* $WVDDeployLogPath\AgentUninstall.txt" -Wait -Passthru
$sts = $legacy_agent_uninstall_status.ExitCode
# Uninstalling previous versions of RDInfraAgent DLLs
Write-Log -Message "Uninstalling any previous versions of RD Infra Agent DLL on VM"
$agent_uninstall_status = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x {CB1B8450-4A67-4628-93D3-907DE29BF78C}", "/quiet", "/qn", "/norestart", "/passive", "/l* $WVDDeployLogPath\AgentUninstall.txt" -Wait -Passthru
$sts = $agent_uninstall_status.ExitCode
# Installing RDInfraAgent
Write-Log -Message "Starting install of $AgentInstaller"
$agent_deploy_status = Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $AgentInstaller", "/quiet", "/qn", "/norestart", "/passive", "REGISTRATIONTOKEN=$RegistrationToken", "/l* $WVDDeployLogPath\AgentInstall.txt" -Wait -Passthru
$sts = $agent_deploy_status.ExitCode
Write-Log -Message "Installing RD Infra Agent on VM Complete. Exit code=$sts"
#FSLogix Install
Write-Log -Message "Starting Install of FSLogix"
$fslgx_deploy_status = Start-Process "$WVDDeployFslgxPath\x64\Release\FSLogixAppsSetup.exe" -ArgumentList "/install /quiet /norestart" -Wait -Passthru
$sts = $fslgx_deploy_status.ExitCode
Write-Log -Message "Installing FSLogix Agent on VM Complete. Exit code=$sts"
#Set Registry Key For Timezone Redirect
$key = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server"
foreach($k in $key){
If ( -Not ( Test-Path "Registry::$k")){New-Item -Path "Registry::$k" -ItemType RegistryKey -Force}
Set-ItemProperty -path "Registry::$k" -Name "fEnableTimeZoneRedirection" -Type "DWord" -Value "1"
}
#Starting Service
Write-Log -Message "Starting RDAgentBootLoader service on SessionHostName"
Start-Service RDAgentBootLoader
#add rdsh vm to hostpool
Write-Log -Message "Adding $SessionHostName To Pool $HostPoolName"
$addRdsh = Set-RdsSessionHost -TenantName $TenantName -HostPoolName $HostPoolName -Name $SessionHostName -AllowNewSession $true
$rdshName = $addRdsh.name | Out-String -Stream
$poolName = $addRdsh.hostpoolname | Out-String -Stream
Write-Log -Message "Successfully added $SessionHostName VM to $HostPoolName"
}
# Get End Time
$endDTM = (Get-Date)
Write-Log -Message "WVD Deploy on $SessionHostName Finished"
# Echo Time elapsed
Write-Log -Message "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"