-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(MODULES-7222) Create a task to list the Update History
This commit adds a script that will query the update history of a Windows computer and then output that information for a bolt or PE Task.
- Loading branch information
1 parent
1bd5928
commit 4bcc461
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
Function Get-SafeString($value) { | ||
if ($value -eq $null) { | ||
Write-Output '' | ||
} else { | ||
Write-Output $value.ToString() | ||
} | ||
} | ||
|
||
Function Get-SafeDateTime($value) { | ||
if ($value -eq $null) { | ||
Write-Output '' | ||
} else { | ||
Write-Output $value.ToString('u') | ||
} | ||
} | ||
|
||
Function Convert-ToServerSelectionString($value) { | ||
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa387280(v=vs.85).aspx | ||
switch ($value) { | ||
0 { Write-Output 'Default' } | ||
1 { Write-Output 'ManagedServer' } | ||
2 { Write-Output 'WindowsUpdate' } | ||
3 { Write-Output 'Other' } | ||
Default { Write-Output "Unknown ${value}"} | ||
} | ||
} | ||
|
||
Function Convert-ToOperationResultCodeString($value) { | ||
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx | ||
switch ($value) { | ||
0 { Write-Output 'Not Started' } | ||
1 { Write-Output 'In Progress' } | ||
2 { Write-Output 'Succeeded' } | ||
3 { Write-Output 'Succeeded With Errors' } | ||
4 { Write-Output 'Failed' } | ||
5 { Write-Output 'Aborted' } | ||
Default { Write-Output "Unknown ${value}"} | ||
} | ||
} | ||
|
||
Function Convert-ToUpdateOperationString($value) { | ||
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa387282(v=vs.85).aspx | ||
switch ($value) { | ||
1 { Write-Output 'Installation' } | ||
2 { Write-Output 'Uninstallation' } | ||
Default { Write-Output "Unknown ${value}"} | ||
} | ||
} | ||
|
||
$Session = New-Object -ComObject "Microsoft.Update.Session" | ||
$Searcher = $Session.CreateUpdateSearcher() | ||
# Returns IUpdateSearcher https://msdn.microsoft.com/en-us/library/windows/desktop/aa386515(v=vs.85).aspx | ||
|
||
$historyCount = $Searcher.GetTotalHistoryCount() | ||
$Searcher.QueryHistory(0, $historyCount) | ForEach-Object -Process { | ||
# Returns IUpdateHistoryEntry https://msdn.microsoft.com/en-us/library/windows/desktop/aa386400(v=vs.85).aspx | ||
$props = @{ | ||
'Operation' = Convert-ToUpdateOperationString $_.Operation | ||
'ResultCode' = Convert-ToOperationResultCodeString $_.ResultCode | ||
'HResult' = $_.HResult | ||
'Date' = Get-SafeDateTime $_.Date | ||
'UpdateIdentity' = @{} | ||
'Title' = Get-SafeString $_.Title | ||
'Description' = Get-SafeString $_.Description | ||
'UnmappedResultCode' = $_.UnmappedResultCode | ||
'ClientApplicationID' = Get-SafeString $_.ClientApplicationID | ||
'ServerSelection' = Convert-ToServerSelectionString $_.ServerSelection | ||
'ServiceID' = Get-SafeString $_.ServiceID | ||
'UninstallationSteps' = @() | ||
'UninstallationNotes' = Get-SafeString $_.UninstallationNotes | ||
'SupportUrl' = Get-SafeString $_.SupportUrl | ||
'Categories' = @() | ||
} | ||
|
||
$_.Categories | % { $props.Categories += $_.Name } | Out-Null | ||
$props['UpdateIdentity']['RevisionNumber'] = $_.UpdateIdentity.RevisionNumber | ||
$props['UpdateIdentity']['UpdateID'] = $_.UpdateIdentity.UpdateID | ||
$_.UninstallationSteps | % { $props.UninstallationSteps += $_ } | Out-Null | ||
|
||
New-Object -TypeName PSObject -Property $props | ||
} | ConvertTo-JSON |