Skip to content

Instantly share code, notes, and snippets.

@trackd
Created July 5, 2023 08:49
Show Gist options
  • Save trackd/c8b4163d4b1e56e057d786e5b3b2b7db to your computer and use it in GitHub Desktop.
Save trackd/c8b4163d4b1e56e057d786e5b3b2b7db to your computer and use it in GitHub Desktop.
Example of running Windows Update check on remote computers
<#
Example of how to get a list of updates that are available to install on a remote computer.
#>
$Scriptblock = {
# create a com object for the update session
$Session = New-Object -ComObject Microsoft.Update.Session
# create a search object for the update session
$Searcher = $Session.CreateupdateSearcher()
# search for updates that are assigned to the computer and not hidden and not installed.
# This can take some time to run.
$AllUpdates = @($Searcher.Search('IsAssigned=1 and IsHidden=0 and IsInstalled=0').Updates)
# create a custom object for each update
foreach ($Update in $AllUpdates) {
[PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Update = $Update.title
Time = $Update.LastDeploymentChangeTime
Status = 'Not Installed'
KB = $Update.KBArticleIDs -join ','
}
}
}
# build a list of computers directly from AD
$ComputerName = Get-ADComputer -Filter 'OperatingSystem -like "Windows Server*" -and Enabled -eq "true"' | Select-Object -ExpandProperty Name
# Can also provide a manual list of computers
# $ComputerName = 'localhost','server01','server02'
# run the scriptblock on the remote computer
$Report = Invoke-Command -ComputerName $ComputerName -ScriptBlock $Scriptblock
# Output the results
$Report | Format-Table -AutoSize
# Optional Export to Excel, using the module ImportExcel
# Install-Module -Name ImportExcel -Scope CurrentUser
$Report | Sort-Object -Property ComputerName | Export-Excel -Path .\ServerUpdates.xlsx -AutoSize -AutoFilter -TableStyle Medium10 -WorksheetName Updates -Title 'Available Updates on Servers' -Show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment