-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-AzureCustomRemoteAppImage.ps1
281 lines (225 loc) · 10.2 KB
/
New-AzureCustomRemoteAppImage.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
279
280
281
<#
.NAME
New-AzureCustomRemoteAppImage.ps1
.SYNOPSIS
Builds an Azure RemoteApp image in an Azure VM
.DESCRIPTION
Used to automate the build process for Azure RemoteApp images
Operations:
- Create VM from a previous specialized image
- Create a new specialized image, a snapshot of the changes performed
- Create a new generalized image to be imported into ARA
- Import image into RemoteApp storage accounts
.VERSION
0.2
.AUTHOR
Morgan Simonsen, Lumagate
.PARAMETER VMHostName
Host name of the VM to create
.PARAMETER CloudServiceName
Name of cloud service to create VM in
We do not check if this exits
.PARAMETER VMSourceImageName
Name of the Azure OS Image to create the new VM from
.PARAMETER VMNewSpecializedImageName
Name of the new specialized image to created, based on the changes the user made in the image
This will server as the source image on the next build cycle
.PARAMETER VMNewGeneralizedImageName
Name of the new generalized image to create and later import into Azure RemoteApp
This is a sysprepped image
.PARAMETER AzureRemoteAppImageName
Name given to the imported generalized image when stored in the Azure RemoteApp image gallery
.EXAMPLE
.\New-AzureCustomRemoteAppImage.ps1 -VMName aratest1 -CloudServiceName tc-ara-build -VMSourceImageName araimgbuild-v02-20150304-206047 -VMNewSpecializedImageName araimgbuild-v03-spec -VMNewGeneralizedImageName araimgbuild-v03-gen
.OUTPUTS
None
.NOTES
- It is probably a good idea NOT to join the VM to a domain
- Use the Azure RemoteApp collection name as a base for the images created; specialized, generalized and Azure RemoteApp Template
E.g.
Specialized : <collectionname>-<version>-spe
Generalized : <collectionname>-<version>-gen
ARA Template image : <collectionname>-<version>
- The VM name, the one visible in the Azure portal, can be anything since the VM container is destroyed when the generalized image is created
.LINK
#>
# Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$VMHostName,
[Parameter(Mandatory=$True,Position=2)]
[string]$CloudServiceName,
[Parameter(Mandatory=$True,Position=3)]
[string]$vNetName,
[Parameter(Mandatory=$True,Position=4)]
[string]$SubnetName,
[Parameter(Mandatory=$True,Position=5,ParameterSetName="GenericImage")]
[string]$VMSourceGenericImageFamilyName="Windows Server RDSHwO365P on Windows Server 2012 R2",
[Parameter(Mandatory=$True,Position=5,ParameterSetName="CustomImage")]
[string]$VMSourceCustomImageName,
[Parameter(Mandatory=$True,Position=6)]
[string]$VMNewSpecializedImageName,
[Parameter(Mandatory=$True,Position=7)]
[string]$VMNewGeneralizedImageName,
[Parameter(Mandatory=$True,Position=8)]
[string]$AzureRemoteAppImageName,
[Parameter(Mandatory=$True,Position=9)]
[string]$AzureRemoteAppImageLocation
)
# Variables
$VMSize = "A5"
$VMConfigurationArchive = "AzureRemoteApp.ps1.zip"
$VMConfigurationName = "AzureRemoteAppImageBuild"
#$vNetName = "SafeRoad-vNET1"
#$SubnetName = "MemberServers"
#$Location = "North Europe"
# Load my custom functions module
Import-Module "..\ManagementInclude.psm1" -Force
# Load credentials
Get-CustomerCredentials
# PSAutomation compatible credentials
# When connecting to VM with remote PowerShell using a local account the New-PSSession cmdlet requires a credential object that
# includes the servername, like server\user. If just username is specified we get access denied
# This object is a second version of the LocalServerCredentials object that includes the servername
$LocalServerCredentials2 = New-Object System.Management.Automation.PSCredential (($VMHostName+"\"+$LocalServerCredentials.UserName), ($LocalServerCredentials.Password))
# Select the Azure subscription from the Current Working Directory (CWD)
# Make sure you are in the right one
#Select-AzureCWDSubscription
Add-AzureAccount
#=====================================================================
# MAIN SCRIPT
#=====================================================================
# Starting up
Write-Host "Starting creation of new Azure RemoteApp image..."
Write-Host "Source image information:"
switch ($PsCmdlet.ParameterSetName)
{
"GenericImage"
{
$SelectedARAImageName = Get-AzureLatestVMImageVersion $VMSourceGenericImageFamilyName
Write-Verbose "Image family: $VMSourceGenericImageFamilyName"
Write-Verbose "Using image: $SelectedARAImageName"
Get-AzureVMImage -ImageName $SelectedARAImageName | Select ImageName,Label,Description
}
"CustomImage"
{
Get-AzureVMImage -ImageName $VMSourceCustomImageName | Select ImageName,Label,Description -ExpandProperty OSDiskConfiguration
}
}
Write-Host "VM Host name : $VMHostName"
Write-Host "Cloud Service : $CloudServiceName"
# Check if VM already exists
Write-Host "Checking for existing VM..."
If (Get-AzureVM -ServiceName $CloudServiceName -Name $VMHostName)
{
Write-Host "VM $VMHostName already exists in Cloud Service $CloudServiceName; quitting!"
Exit
}
Else
{
Write-Host "VM $VMHostName not found; continuing..."
}
# Configure and provision VM
Write-Host "Provisioning VM..."
switch ($PsCmdlet.ParameterSetName)
{
"GenericImage"
{
New-AzureQuickVM -Windows -Name $VMHostName -ServiceName $CloudServiceName -ImageName $SelectedARAImageName -WaitForBoot -VNetName $vNetName -SubnetNames $SubnetName -InstanceSize $VMSize -AdminUsername $LocalServerCredentials.UserName -Password ($LocalServerCredentials.GetNetworkCredential().password)
}
"CustomImage"
{
New-AzureQuickVM -Windows -Name $VMHostName -ServiceName $CloudServiceName -ImageName $VMSourceCustomImageName -WaitForBoot -VNetName $vNetName -SubnetNames $SubnetName -InstanceSize $VMSize
}
}
# Get new VM object
$NewVM = Get-AzureVM -ServiceName $CloudServiceName -Name $VMHostName
# Download RDP file
Write-Host "Downloading RDP file..."
Get-AzureRemoteDesktopFile -ServiceName $CloudServiceName -Name $VMHostName -LocalPath ([environment]::getfolderpath("UserProfile")+"\Downloads\"+$VMHostName+".rdp")
# Prompt to customize
$RemoteDesktopEndpoint = $NewVM | Get-AzureEndpoint | where { $_.LocalPort -eq 3389 }
# Wait for VM RDP Endpoint to become available
Do
{
Try
{
(New-Object System.Net.Sockets.TcpClient).Connect($CloudServiceName,$RemoteDesktopEndpoint)
$RDPOK=$true
}
Catch
{
Start-Sleep 5
$RDPOK=$false
}
}
Until ( $RDPOK=$true )
Write-Host "VM ready for user customization, connect using downloaded RDP file."
Write-Host "Internal IP address: " $NewVM.IpAddress
Write-Host "Cloud service connection information: " ($CloudServiceName+":"+($RemoteDesktopEndpoint).ToString())
PressKeyToContinue -pauseKey "I" -modifier Alt -hideKeysStrokes:$true
#Shut down VM before specialized capture
Write-Host "Shutting down VM in preparation of specialized capture..."
$NewVM | Stop-AzureVM -StayProvisioned:$true
# Wait for VM to shut down
Do
{
$NewVM = Get-AzureVM -ServiceName $CloudServiceName -Name $VMHostName
Start-Sleep 5
}
Until ( $NewVM.InstanceStatus -eq "StoppedVM" )
# Save specialized copy
Write-Host "Starting specialized capture..."
$NewVM | Save-AzureVMImage –ImageName $VMNewSpecializedImageName –OSState "Specialized"
Write-Host "Starting VM..."
$NewVM | Start-AzureVM
# Wait for VM to become ready
Do
{
$NewVM = Get-AzureVM -ServiceName $CloudServiceName -Name $VMHostName
Start-Sleep 5
}
Until ( $NewVM.InstanceStatus -eq "ReadyRole" )
# Must wait longer here; or check that PS endpoint/connection works
# Run sysprep in VM
Write-Host "Running sysprep in VM..."
$pso = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
$PowerShellEndpoint = $NewVM | Get-AzureEndpoint | where { $_.LocalPort -eq 5986 }
$VMSession = New-PSSession -ComputerName ($NewVM.ServiceName+".cloudapp.net") -Credential $LocalServerCredentials2 -SessionOption $pso -UseSSL -Port $PowerShellEndpoint.Port
Invoke-Command -Session $VMSession -ScriptBlock { Set-Content -Value ((gc env:systemroot)+"\system32\sysprep\sysprep.exe /oobe /generalize /shutdown") -Path runsysprep.cmd }
#Invoke-Command -Session $VMSession -ScriptBlock { If (!(Get-WURebootStatus -Silent)) { Start-Process -FilePath .\runsysprep.cmd -WindowStyle Normal -Verbose} Else { Write-Host "A reboot is pending, cannot run sysprep. Manually restart server and run sysprep" } }
Invoke-Command -Session $VMSession -ScriptBlock { Start-Process -FilePath .\runsysprep.cmd -WindowStyle Normal -Verbose}
#PressKeyToContinue -pauseKey "I" -modifier Alt -hideKeysStrokes:$true
# Save specialized copy
Write-Host "Starting generalized capture..."
# Wait for VM to shut down
Do
{
$NewVM = Get-AzureVM -ServiceName $CloudServiceName -Name $VMHostName
Start-Sleep 5
}
Until ( $NewVM.InstanceStatus -eq "StoppedVM" )
$NewVM | Save-AzureVMImage –ImageName $VMNewGeneralizedImageName –OSState "Generalized"
# Import custom image into Azure RemoteApp storage
Write-Host "Importing VM image to Azure RemoteApp storage..."
#.\Import-AzureRemoteAppTemplateImage.ps1 -AzureVMImageName $VMNewGeneralizedImageName -RemoteAppTemplateImageName $AzureRemoteAppImageName
New-AzureRemoteAppTemplateImage -ImageName $AzureRemoteAppImageName -Location $AzureRemoteAppImageLocation -AzureVmImageName $VMNewGeneralizedImageName
# Wait for image import to finish
[int]$s = 60
Write-Host "Waiting for image import to finish... (Checking status every $s seconds)"
Do
{
$AzureRemoteAppImageImportStatus = (Get-AzureRemoteAppTemplateImage -ImageName $AzureRemoteAppImageName).Status
Write-Host " Image upload status: $AzureRemoteAppImageImportStatus"
Start-Sleep $s
}
Until ($AzureRemoteAppImageImportStatus -eq "Ready")
# Update Azure RemoteApp service
#Write-Host "Importing image into Azure RemoteApp deployment..."
#Update-AzureRemoteAppService -RemoteAppServiceName $AzureRemoteAppServiceName -RemoteAppTemplateImageName $AzureRemoteAppImageName
Write-Host "Done!"
Write-Host "Specialized image name :" $VMNewSpecializedImageName
Write-Host "Generalized image name :" $VMNewGeneralizedImageName
Write-Host "Azure RemoteApp image name :" $AzureRemoteAppImageName