forked from lucabol/DTLCustomImagesLab
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Apply-WindowsUpdateArtifactToVMs.ps1
60 lines (45 loc) · 2.28 KB
/
Apply-WindowsUpdateArtifactToVMs.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
param
(
[Parameter(Mandatory=$false, HelpMessage="Configuration File, see example in directory")]
[ValidateNotNullOrEmpty()]
[string] $ConfigFile = "config.csv",
[Parameter(HelpMessage="Example: 'ID-*,CSW2-SRV' , a string containing comma delimitated list of patterns. The script will (re)create just the VMs matching one of the patterns. The empty string (default) recreates all labs as well.")]
[string] $ImagePattern = "",
[Parameter(Mandatory=$false, HelpMessage="Shutdown the VMs when done?")]
[ValidateNotNullOrEmpty()]
[boolean] $shutdownVMs = $false
)
$ErrorActionPreference = "Stop"
. "./Utils.ps1"
Import-AzDtlModule
# Read in the config file
$config = Import-Csv $ConfigFile
# Foreach lab, get all the VMs
$config | ForEach-Object {
Write-Host "----------------------------------------------------------" -ForegroundColor Green
Write-Host "Applying Windows Updates to all VMs in DevTestLab: $($_.DevTestLabName) matching pattern: '$($ImagePattern)'" -ForegroundColor Green
$vms = Get-AzDtlVm -Lab @{Name = $_.DevTestLabName; ResourceGroupName = $_.ResourceGroupName}
# Scope down the list of VMs based on the image pattern
$vms = Select-Vms -vms $vms -ImagePattern $ImagePattern
Write-Host "Status of VMs before doing any operations..."
$vms | Select-Object `
@{label="Name";expression={$_.Name}},
@{label="ProvisioningState";expression={$_.Properties.provisioningState}},
@{label="PowerState";expression={$_.Properties.lastKnownPowerState}},
@{label="ArtifactStatus";expression={$_.Properties.ArtifactDeploymentStatus.deploymentStatus}} `
| Format-Table
# we just wait for this without status
$vms | Start-AzDtlVm -AsJob `
| Receive-Job -Wait `
| Out-Null
# Apply artifacts to all the VMs, wait with status for 40 min
$jobs = $vms | Set-AzDtlVmArtifact -RepositoryName "Public Repo" -ArtifactName "windows-install-windows-updates" -AsJob
Wait-JobWithProgress -jobs $jobs -secTimeout 14400
if ($shutdownVMs) {
# next we shutdown all the VMs
$vms | Stop-AzDtlVm -AsJob `
| Receive-Job -Wait `
| Out-Null
}
Write-Host "Completed script to apply Windows Updates to all VMs" -ForegroundColor Green
}