-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeployNewImage.ps1
More file actions
executable file
·150 lines (130 loc) · 6.03 KB
/
deployNewImage.ps1
File metadata and controls
executable file
·150 lines (130 loc) · 6.03 KB
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
param (
[string]$projectId = $(throw "-projectId is required.")
)
Update-AzConfig -DisplayBreakingChangeWarning $false | out-null
$ErrorActionPreference = "Stop"
$rgName = $projectId + "-RG"
$hostPool = $projectId + "-HP"
$location = "westus"
function Show-PoolHosts{
""
$currentHosts = Get-AzWvdSessionHost -ResourceGroupName $rgName -HostPoolName $hostPool
$format = "{0,-35}{1,-13}{2,-10}{3,-15}{4}"
$format -f "Name", "Status", "Sessions", "Image Version", "VM Power State"
foreach ($ahost in $currentHosts){
$vmPowerState = (Get-AzVM -ResourceId $ahost.ResourceId -Status).Statuses[1].Code
$imgVer = (Get-AzVM -ResourceId $ahost.ResourceId).StorageProfile.ImageReference.ExactVersion
$format -f $ahost.Name, $ahost.Status, $ahost.Session, $imgVer, $vmPowerState
}
""
}
Show-PoolHosts
#
# Determine what is the latest golden image version in the Gallery.
#
$imageGalery = $projectId + "_Galery"
$imgVersions = Get-AzGalleryImageVersion -ResourceGroupName $rgName -GalleryName $imageGalery -GalleryImageDefinitionName Windows11MultiUser-VDI-Apps
$latestImage = $imgVersions | select -first 1
foreach ($ver in $imgVersions){
if ($latestImage.PublishingProfile.PublishedDate -lt $ver.PublishingProfile.PublishedDate){
$latestImage = $ver
}
}
#
# Determine how many of the current hosts were deployed using and older image version.
#
$activeHosts = Get-AzWvdSessionHost -ResourceGroupName $rgName -HostPoolName $hostPool | where {$_.AllowNewSession -eq $true}
foreach ($ahost in $activeHosts){
$vm = Get-AzVM -ResourceId $ahost.ResourceId
"Current SessionHost version: " + $vm.StorageProfile.ImageReference.ExactVersion
"Most recent version name: " + $latestImage.name
if ($vm.StorageProfile.ImageReference.ExactVersion -ne $latestImage.name){
$hostsToReplace += 1
}
}
#
# If there are no hosts created with an older image version, end the script.
#
if ( $hostsToReplace -eq 0 ){
"All host are at the latest image version"
Exit
}
"Number of hosts to replace: " + $hostsToReplace
#
# Get the information required to deploy new hosts, i.e, Pool registration key, VNet info, Username and Password
#
$registrationInfo = New-AzWvdRegistrationInfo `
-ResourceGroupName $rgName `
-HostPoolName $hostPool `
-ExpirationTime $((get-date).ToUniversalTime().AddDays(1).ToString('yyyy-MM-ddTHH:mm:ss.fffffffZ'))
$Vnet = Get-AzVirtualNetwork -Name "VDIVnet" -ResourceGroupName $rgName
#
# Grant KeyVault access to the current public IP and retrieve the VDI host username and password, and remove access when done.
#
$vault = $projectId + "-KV"
$pubIp = (Invoke-WebRequest -uri "https://api.ipify.org/").Content
Add-AzKeyVaultNetworkRule -VaultName $vault -IpAddressRange $pubIp
$vdiHostAdminUsername = Get-AzKeyVaultSecret -VaultName $vault -Name vdiHostAdminUsername -AsPlainText
$textPassword = Get-AzKeyVaultSecret -VaultName $vault -Name vdiHostAdminPassword -AsPlainText
Remove-AzKeyVaultNetworkRule -VaultName $vault -IpAddressRange $pubIp
$vdiHostAdminPassword = ConvertTo-SecureString $textPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($vdiHostAdminUsername, $vdiHostAdminPassword);
$justId = $projectId -replace "[^0-9]" , ''
#
# Include the image version number in the vm name.
#
$vmName = "sh" + $justId + "v" + $latestImage.name.Replace(".","") + "-"
#
# Create as many hosts as there are currently with dated image versions.
#
for($i = 1;$i -le $hostsToReplace;$i++)
{
"Deploying host " + $i + " of " + $hostsToReplace
$newVMName = $vmName + $i
$NICName = $newVMName + "VMNic"
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $rgName -Location $Location -SubnetId $Vnet.Subnets[0].Id
$VM = New-AzVMConfig -VMName $newVMName -VMSize "Standard_DS1_v2" -IdentityType SystemAssigned
$VM = Set-AzVMOperatingSystem -VM $VM -Windows -ComputerName $newVMName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VM = Set-AzVMOSDisk -VM $VM -DeleteOption Delete -CreateOption FromImage
$VM = Set-AzVMBootDiagnostic -VM $VM -Disable
$VM = Add-AzVMNetworkInterface -VM $VM -Id $NIC.Id -DeleteOption Delete
$VM = Set-AzVMSourceImage -VM $VM -Id $latestImage.id
"Deploying VM " + $newVMName
New-AzVM -ResourceGroupName $rgName -Location $location -VM $VM -LicenseType Windows_Client -DisableBginfoExtension
"Joining VM " + $newVMName + " to AAD"
Set-AzVMExtension `
-ResourceGroupName $rgName `
-VMName $newVMName `
-Name "AADLoginForWindows" `
-Location $VM.Location `
-Publisher "Microsoft.Azure.ActiveDirectory" `
-Type "AADLoginForWindows" `
-TypeHandlerVersion "0.4"
"Adding VM " + $newVMName + " to Host Pool"
$cmdResult = Invoke-AzVMRunCommand `
-ResourceGroupName $rgName `
-Name $newVMName `
-CommandId 'RunPowerShellScript' `
-ScriptPath 'setWVDClient.ps1' `
-Parameter @{registrationtoken = $registrationInfo.Token}
$cmdResult.Value[0].Message
}
#
# Disable and deallocate previous-version hosts.
#
foreach ($shost in $activeHosts){
$vm = Get-AzVM -ResourceId $shost.ResourceId
if ($vm.StorageProfile.ImageReference.ExactVersion -ne $latestImage.name){
"Disabling new sessions on VM: " + $vm.Name
Update-AzWvdSessionHost -ResourceGroupName $rgName `
-HostPoolName $hostPool `
-Name $vm.Name `
-AllowNewSession:$false `
| select Name, Session, Status, AllowNewSession | ft
if ($shost.Session -eq 0){
"Stopping VM: " + $vm.Name
Stop-AzVM -Id $vm.Id -Force
}
}
}
Show-PoolHosts