Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Defines update criteria: we're interested in "software" (i.e. Windows/ Office, not drivers or anything) that are not yet installed
- $newUpdateCriteria = "IsInstalled=0 and Type='Software'"
- #Search for new updates.
- $newUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
- $newSearchResults = $newUpdateSearcher.Search($newUpdateCriteria)
- If ($newSearchResults.updates.Count -eq 0)
- {
- Write-Host "I couldn't find any updates..."
- Start-Sleep 5
- }
- Else
- {
- #Download updates.
- $updateSession = New-Object -ComObject Microsoft.Update.Session #creates a new update session object
- $downloader = $updateSession.CreateUpdateDownloader() #creates an object for downloading the updates
- $downloader.Updates = $newSearchResults.Updates #assigns the objects found above to the download object
- $downloader.Download() #downloads the updates
- #Install updates
- $installer = New-Object -ComObject Microsoft.Update.Installer #creates a new update installer object
- $installer.Updates = $newSearchResults.Updates #uses the updates from the initial search to populate what to download
- $installUpdates = $Installer.Install() #installs the updates
- #Reboot if required by updates.
- If ($installUpdates.RebootRequired -eq $true)
- {
- Write-Host "This computer needs to be restarted"
- }
- Else
- {
- Write-Host "Reboot NOT required!"
- Start-Sleep 5
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment