Not sure if I am posting to the right forum category.
I would like to automate Windows Updates for our physical and virtual servers. I am thinking about Powershell scripting in SimpleHelp Toolboxes. A script that will weekly check for available critical updates, install them and blocks / delays automatic reboot. Automatic reboot is a bad idea in our environment, some servers need Domain Controller during their startup.
Has anybody seen a script like this? Or is there a better approach for Windows Updates and Patch Management automation?
In general, all Patch Management ideas are welcome.
1 Like
Iâd also love to know this as wellâŚ
I use this PS script:
------------------------------------------------------------------
PowerShell Script To Automate Windows Update
Script should be executed with âAdministratorâ Privilege
------------------------------------------------------------------
$Today = Get-Date
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session
Write-Host
Write-Host â`t Initializing and Checking for Applicable Updates. Please wait âŚâ -ForeGroundColor âYellowâ
$Result = $Searcher.Search(âIsInstalled=0 and Type=âSoftwareâ and IsHidden=0â)
If ($Result.Updates.Count -EQ 0) {
Write-Host ât There are no applicable updates for this computer." } Else { $ReportFile = $Env:ComputerName + "_Report.txt" If (Test-Path $ReportFile) { Remove-Item $ReportFile } New-Item $ReportFile -Type File -Force -Value "Windows Update Report For Computer: $Env:ComputerName
rn" | Out-Null Add-Content $ReportFile "Report Created On: $Today
râ
Add-Content $ReportFile â==============================================================================r
nâ
Write-Host ât Preparing List of Applicable Updates For This Computer ..." -ForeGroundColor "Yellow" Add-Content $ReportFile "List of Applicable Updates For This Computer
râ
Add-Content $ReportFile â------------------------------------------------r" For ($Counter = 0; $Counter -LT $Result.Updates.Count; $Counter++) { $DisplayCount = $Counter + 1 $Update = $Result.Updates.Item($Counter) $UpdateTitle = $Update.Title Add-Content $ReportFile "
t $DisplayCount â $UpdateTitleâ
}
$Counter = 0
$DisplayCount = 0
Add-Content $ReportFile âr
nâ
Write-Host ât Initializing Download of Applicable Updates ..." -ForegroundColor "Yellow" Add-Content $ReportFile "Initializing Download of Applicable Updates" Add-Content $ReportFile "------------------------------------------------
râ
$Downloader = $Session.CreateUpdateDownloader()
$UpdatesList = $Result.Updates
For ($Counter = 0; $Counter -LT $Result.Updates.Count; $Counter++) {
$UpdateCollection.Add($UpdatesList.Item($Counter)) | Out-Null
$ShowThis = $UpdatesList.Item($Counter).Title
$DisplayCount = $Counter + 1
Add-Content $ReportFile ât $DisplayCount -- Downloading Update $ShowThis
râ
$Downloader.Updates = $UpdateCollection
$Track = $Downloader.Download()
If (($Track.HResult -EQ 0) -AND ($Track.ResultCode -EQ 2)) {
Add-Content $ReportFile ât Download Status: SUCCESS" } Else { Add-Content $ReportFile "
t Download Status: FAILED With Error â $Error()â
$Error.Clear()
Add-content $ReportFile âr" } } $Counter = 0 $DisplayCount = 0 Write-Host "
t Starting Installation of Downloaded Updates âŚâ -ForegroundColor âYellowâ
Add-Content $ReportFile âr
nâ
Add-Content $ReportFile âInstallation of Downloaded Updatesâ
Add-Content $ReportFile â------------------------------------------------r" $Installer = New-Object -ComObject Microsoft.Update.Installer For ($Counter = 0; $Counter -LT $UpdateCollection.Count; $Counter++) { $Track = $Null $DisplayCount = $Counter + 1 $WriteThis = $UpdateCollection.Item($Counter).Title Add-Content $ReportFile "
t $DisplayCount â Installing Update: $WriteThisâ
$Installer.Updates = $UpdateCollection
Try {
$Track = $Installer.Install()
Add-Content $ReportFile ât Update Installation Status: SUCCESS" } Catch { [System.Exception] Add-Content $ReportFile "
t Update Installation Status: FAILED With Error â $Error()â
$Error.Clear()
Add-content $ReportFile â`râ
}
}
}
exit 0
Works on Win10 and Server 2012 or newer.
Major feature updates (e.g.: 1803 to 1907 upgrade) doesnât work on - user has to touch the machine for that. Otherwise, it works well.
Thank you!
Is there a way to get a run down on machine out of date and which updates are missing?
Jarred_Wheeler, thanks for sharing your script. I am thinking about putting it into SimpleHelp toolbox. Will this work? How to ensure starting with Admin privilege?
I found some time to test few other Windows Updates automation ideas. We run a mix of Windows Server OSs on our servers: 2008-2019. It is difficult to get a single command working for all of them.
SImplehelp has a build-in âRun Windows Updatesâ command in toolbox (wuauclt.exe /detectnow /updatenow). This method does not work for most of my servers.
PSWindowsupdate module, sounds very promising. Not best match for my environment. It requires multiple components to be at the same fresh versions: Powershell, WMF, dotnet etc. Difficult to achieve in our Windows OS zoo.
ABC-Update FreeWare, command line version. Works best for me now. It still needs dot net 2, no problem to enable on all my servers. Easy to fine- tune actions like list missing updates only or list and install them, save log to file or send via email.
Apologies for the late reply.
The windows agent runs with highest privileges regardless (as per SH support).
The script works on anything windows 10 1709 and newer. Iâve tested it on Server 2008+ but the latest Powershell has to be installed to understand some cmdlets.
MacOS is the one Iâm running into issues with on sudoâing.
There are a few ps1 files on TechNet that will assist with this.
I donât really care about knowing what updates are available - just so that they get installed.
On the other hand, I care about the updates that are available for MacOS - of which is a MUCH easier script to pull that info.
James_Fouracre> Is there a way to get a run down on machine out of date and which updates are missing?
James, there are multiple PowerShell modules and scripts available for this type of reporting (as Jarred_Wheeler mentions). It is sometimes a challenge to adopt them for your needs.
I use abc-udate for listing missing updates (per computer), that small util can even email the report. Still interested in getting a report for my all servers.
I have an idea - collect individual logs and parsing them in a consolidated report. With colors (green, yellow and red) to indicate amount of missing updates, managers will be happy.
1 Like