Powershell - install updates for Windows?

Is it possible?

I think we will need to somehow call WUAgent to start the detection, but I would like to download and install updates, and then reboot as part of the script.

This will be part of a larger script to basically build the vanilla block 2008R2 before DC via Powershell.

+5
source share
2 answers

Take a look at the PSWindowsUpdate module for PowerShell.

It is located here at the Script Center.

+9
source

I suggest using this script

Function WSUSUpdate {
$Criteria = "IsInstalled=0 and Type='Software'"
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
try {
    $SearchResult = $Searcher.Search($Criteria).Updates
    if ($SearchResult.Count -eq 0) {
        Write-Output "There are no applicable updates."
        exit
    } 
    else {
        $Session = New-Object -ComObject Microsoft.Update.Session
        $Downloader = $Session.CreateUpdateDownloader()
        $Downloader.Updates = $SearchResult
        $Downloader.Download()
        $Installer = New-Object -ComObject Microsoft.Update.Installer
        $Installer.Updates = $SearchResult
        $Result = $Installer.Install()
    }
}
catch {
    Write-Output "There are no applicable updates."
    }
}

WSUSUpdate
If ($Result.rebootRequired) { Restart-Computer }

Source: https://gist.github.com/jacobludriks/9ca9ce61de251a5476f1

+2
source

All Articles