question

DucheminDominique-7551 avatar image
0 Votes"
DucheminDominique-7551 asked DucheminDominique-7551 answered

Windows Updates Powershell command

Hello,

I was using this command:

$lastpatch = (get-hotfix -computername $hostname | select InstalledOn | Sort-Object installedon -Descending | Select-Object -first 1).installedon;

or
PS C:\Windows\system32> get-hotfix -computername SOPCPCWS1 | select InstalledOn | Sort-Object installedon -Descending |
Select-Object -first 1

InstalledOn

1/24/2021 12:00:00 AM

and I tried also

> Get-wmiobject -class win32_quickfixengineering | Sort InstalledOn

Samething the KN890830 is not listed and as is the last windows updates is incorrect!!!

But I noticed that the "Windows Malicious Software Removal Too x64 - v5.96 (KB890830) is not detected as it is not a hotfix ...

how to get the last "windows updates" run through powershell? (including the KB890830)

Thanks,
Dom
163667-2022-01-10-9-49-34-sopcpcws1-windows-updates.png163659-2022-01-10-10-04-32-sopcpcws1-updates.png





windows-serverwindows-server-powershellwindows-server-update-services
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered

You could try something like this:

 # Convert Wua History ResultCode to a Name 
 # 0, and 5 are not used for history 
 # See https://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx
    
 function Convert-WuaResultCodeToName {
     param( [Parameter(Mandatory = $true)]
         [int] $ResultCode
     )
     # https://docs.microsoft.com/en-us/windows/win32/api/wuapi/ne-wuapi-operationresultcode
     $Result = $ResultCode
     switch ($ResultCode) {
         0 { $Result = "NotStarted; break"}
         1 { $Result = "InProgress"; break }
         2 { $Result = "Succeeded"; break }
         3 { $Result = "Succeeded With Errors"; break }
         4 { $Result = "Failed"; break }
         5 { $Result = "Aborted"; break }
     }
     $Result
 }
 function Get-WuaHistory {
     # Get a WUA Session
     $session = New-Object -ComObject 'Microsoft.Update.Session'
     $searcher = $session.CreateUpdateSearcher()
     $historyCount = $searcher.GetTotalHistoryCount()
     # Query the latest 1000 History starting with the first record
     $session.QueryHistory("", 0, $historyCount) |  # criteria, start index, count
         ForEach-Object {
             [PSCustomObject]@{
                 Result = (Convert-WuaResultCodeToName -ResultCode $_.ResultCode)
                 Date = $_.Date
                 Title = $_.Title
                 SupportUrl = $_.SupportUrl
                 Product = ($_.Categories | Where-Object { $_.Type -eq 'Product' } | Select-Object -First 1 -ExpandProperty Name)
                 UpdateId = ($_.UpdateIdentity.UpdateId)
                 RevisionNumber = ($_.UpdateIdentity.RevisionNumber)
                 Description = $_.Description
             }
         } | Where-Object { ![String]::IsNullOrWhiteSpace($_.title) }     #remove null(?) records, return selected properties
 }
    
 Get-WuaHistory | export-csv c:\junk\updates.csv -notypeinfo #Format-Table -AutoSize
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DucheminDominique-7551 avatar image
0 Votes"
DucheminDominique-7551 answered

Hello,

Should it work in Windows Server 2008?
I tested in Windows Server 2016 and 2019 it is working fine as well as Windows 10.
But Windows Server 2008 and Windows Server 2008 R2 does not give any result as I was not able to run at least the command:
Install-Module -name PSWindowsUpdate
PS C:\Windows\system32> import-module -name PSWindowsUpdate
Import-Module : The specified module 'PSWindowsUpdate' was not loaded because no valid module file was found in any mod
ule directory.
At line:1 char:14
+ import-module <<<< -name PSWindowsUpdate
+ CategoryInfo : ResourceUnavailable: (PSWindowsUpdate:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand


Checking for the download...

Thanks,
Dom

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DucheminDominique-7551 avatar image
0 Votes"
DucheminDominique-7551 answered

Hello,

I find out that this issue was due to the ESU licensing not renewed and as is the patches were not picked/listed. After renewing the ESU license this is working and patches came available and applied...

Thanks,
Dom

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.