-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindows-update-history.ps1
36 lines (33 loc) · 1.2 KB
/
windows-update-history.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
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
trap {
Write-Output "ERROR: $_"
Write-Output (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1')
Write-Output (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1')
Exit 1
}
Write-Output 'Windows update history:'
$updateSearcher = (New-Object -ComObject 'Microsoft.Update.Session').CreateUpdateSearcher()
$updateSearcher.QueryHistory(0, $updateSearcher.GetTotalHistoryCount()) `
| Where-Object {$_.ResultCode -ne $null} `
| ForEach-Object {
$result = switch ($_.ResultCode) {
0 { 'NotStarted' }
1 { 'InProgress' }
2 { 'Succeeded' }
3 { 'SucceededWithErrors' }
4 { 'Failed' }
5 { 'Aborted' }
default { $_ }
}
New-Object -TypeName PSObject -Property @{
Date = $_.Date
By = $_.ClientApplicationID
Result = $result
Title = $_.Title
}
} `
| Sort-Object -Descending Date `
| Format-Table -Property Date,Result,By,Title -AutoSize `
| Out-String -Stream -Width ([int]::MaxValue) `
| ForEach-Object {$_.TrimEnd()}