Powershell Automated Windows Updater Script only Security Updates & Rollup Filter!! (Windows Server )

I came across for the need  to automating updates and filtering by Security & Monthly Rollup only using a PowerShell Script ;

This needs to be run as admin and does not require any module to be run;

It has been tested on Windows Server 2012/R2/2016

 

# To create the searcher we use new Object , we store the returned Searcher #object in the $Searcher variable
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
#Create the session with MS update
$Session = New-Object -ComObject Microsoft.Update.Session
#UpdateCollection Object to Store Windows updates ID based on our criteria
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
# To install a software Update we need to search for the updated and add it to #the collection , download & install . we canuse Update Identity to easy #retrieve UpdateID
$results = $searcher.search("Type='software' AND IsInstalled = 0")
#$Results.Updates |
#ForEach-Object { $_.Identity.UpdateID ; "`t" + $_.Title }
#We Explore the results from object the previous Searcher and we define title #needs to be Security or Rollup!
$Results.Updates |
ForEach-Object{
if (($_.Title -like '*Preview of Monthly Quality Rollup for Windows Server*') -or ($_.Title -like '*Security Monthly Quality Rollup for Windows Server*'))
{
#echo "yes, found "
echo $_.Title
#echo $_.Identity.UpdateID
#Variable to Store identity of the Update ID
$updateID= $_.Identity.UpdateID
#Redefine Result with the wanted UPDATE ID
$Result = $Searcher.Search("UpdateID='$updateID'")
$Updates = $Result.updates
#We do Add to the Update collection
$UpdateCollection.Add($Updates.Item(0)) | out-null
# Creating the Downloader
$Downloader = $Session.CreateUpdateDownloader()
#Passing the collection to the downloader
$Downloader.Updates = $UpdateCollection
#Downloading Updates
$Downloader.Download()
# Creating The object Installer
$Installer = New-Object -ComObject Microsoft.Update.Installer
#Passing the Installer to the update collection
$Installer.Updates = $UpdateCollection
#Installing the Updates
$Installer.Install()
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.