-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathinstall-updates.ps1
87 lines (71 loc) · 2.95 KB
/
install-updates.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
# Source : https://github.com/StefanZ8n/packer-ws2022/blob/main/scripts/win-update.ps1
# Silence progress bars in PowerShell, which can sometimes feed back strange
# XML data to the Packer output.
$ProgressPreference = "SilentlyContinue"
Write-Output "***** Starting PSWindowsUpdate Installation"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name PSWindowsUpdate -Force
if (Get-ChildItem "C:\Program Files\WindowsPowerShell\Modules\PSWindowsUpdate") {
Write-Output "***** PSWindowsUpdate installed successfully"
}
Write-Output "***** Starting Windows Update Installation"
Try
{
Import-Module PSWindowsUpdate -ErrorAction Stop
}
Catch
{
Write-Error "***** Unable to install PSWindowsUpdate"
exit 1
}
if (Test-Path C:\Windows\Temp\PSWindowsUpdate.log) {
# Save old logs
Rename-Item -Path C:\Windows\Temp\PSWindowsUpdate.log -NewName PSWindowsUpdate-$((Get-Date).Ticks).log
# Uncomment the line below to delete old logs instead
#Remove-Item -Path C:\Windows\Temp\PSWindowsUpdate.log
}
try {
$updateCommand = {ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll -IgnoreReboot -Install | Out-File C:\Windows\Temp\PSWindowsUpdate.log}
$TaskName = "PackerUpdate"
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
$Scheduler = New-Object -ComObject Schedule.Service
$Task = $Scheduler.NewTask(0)
$RegistrationInfo = $Task.RegistrationInfo
$RegistrationInfo.Description = $TaskName
$RegistrationInfo.Author = $User.Name
$Settings = $Task.Settings
$Settings.Enabled = $True
$Settings.StartWhenAvailable = $True
$Settings.Hidden = $False
$Action = $Task.Actions.Create(0)
$Action.Path = "powershell"
$Action.Arguments = "-Command $updateCommand"
$Task.Principal.RunLevel = 1
$Scheduler.Connect()
$RootFolder = $Scheduler.GetFolder("\")
$RootFolder.RegisterTaskDefinition($TaskName, $Task, 6, "SYSTEM", $Null, 1) | Out-Null
$RootFolder.GetTask($TaskName).Run(0) | Out-Null
Write-Output "***** The Windows Update log will be displayed below this message. No additional output indicates no updates were needed."
do {
sleep 1
if ((Test-Path C:\Windows\Temp\PSWindowsUpdate.log) -and $script:reader -eq $null) {
$script:stream = New-Object System.IO.FileStream -ArgumentList "C:\Windows\Temp\PSWindowsUpdate.log", "Open", "Read", "ReadWrite"
$script:reader = New-Object System.IO.StreamReader $stream
}
if ($script:reader -ne $null) {
$line = $Null
do {$script:reader.ReadLine()
$line = $script:reader.ReadLine()
Write-Output $line
} while ($line -ne $null)
}
} while ($Scheduler.GetRunningTasks(0) | Where-Object {$_.Name -eq $TaskName})
} finally {
$RootFolder.DeleteTask($TaskName,0)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Scheduler) | Out-Null
if ($script:reader -ne $null) {
$script:reader.Close()
$script:stream.Dispose()
}
}
Write-Output "***** Ended Windows Update Installation"