[Windows] Update helper function that returns Windows Update states (#8878)

This commit is contained in:
Vasilii Polikarpov
2023-11-27 12:29:42 +01:00
committed by GitHub
parent 12066050d0
commit e1e621e78c
5 changed files with 49 additions and 29 deletions

View File

@@ -595,14 +595,33 @@ function Get-AndroidInstalledPackages {
return (cmd /c "$sdkManager --list_installed 2>&1") -notmatch "Warning"
}
function Get-WindowsUpdatesHistory {
$allEvents = @{}
# 19 - Installation Successful: Windows successfully installed the following update
# 20 - Installation Failure: Windows failed to install the following update with error
# 43 - Installation Started: Windows has started installing the following update
function Get-WindowsUpdateStates {
<#
.SYNOPSIS
Retrieves the status of Windows updates.
.DESCRIPTION
The Get-WindowsUpdateStates function checks the Windows Event Log for specific event IDs related to Windows updates and returns a custom PowerShell object with the state and title of each update.
.PARAMETER None
This function does not take any parameters.
.OUTPUTS
PSCustomObject. This function returns a collection of custom PowerShell objects. Each object has two properties:
- State: A string that represents the state of the update. Possible values are "Installed", "Failed", and "Running".
- Title: A string that represents the title of the update.
.NOTES
Event IDs used:
- 19: Installation Successful: Windows successfully installed the following update
- 20: Installation Failure: Windows failed to install the following update with error
- 43: Installation Started: Windows has started installing the following update
#>
$completedUpdates = @{}
$filter = @{
LogName = "System"
Id = 19, 20, 43
LogName = "System"
Id = 19, 20, 43
ProviderName = "Microsoft-Windows-WindowsUpdateClient"
}
$events = Get-WinEvent -FilterHashtable $filter -ErrorAction SilentlyContinue | Sort-Object Id
@@ -610,30 +629,31 @@ function Get-WindowsUpdatesHistory {
foreach ( $event in $events ) {
switch ( $event.Id ) {
19 {
$status = "Successful"
$state = "Installed"
$title = $event.Properties[0].Value
$allEvents[$title] = ""
$completedUpdates[$title] = ""
break
}
20 {
$status = "Failure"
$state = "Failed"
$title = $event.Properties[1].Value
$allEvents[$title] = ""
$completedUpdates[$title] = ""
break
}
43 {
$status = "InProgress"
$state = "Running"
$title = $event.Properties[0].Value
break
}
}
if ( $status -eq "InProgress" -and $allEvents.ContainsKey($title) ) {
# Skip update started event if it was already completed
if ( $state -eq "Running" -and $completedUpdates.ContainsKey($title) ) {
continue
}
[PSCustomObject]@{
Status = $status
State = $state
Title = $title
}
}