7

Is this possible?

I guess we would need to invoke the WUAgent somehow to run a detection, but I would like to essentially download and install updates, then reboot as part of the script.

This will be part of a larger script to basically build a vanilla 2008R2 box up to a DC all through Powershell.

1
  • Because the same .NET methods available via C# are available in PowerShell (including namespace declarations), what the answers to stackoverflow.com/q/922132 describe may work. Commented Apr 7 at 23:57

3 Answers 3

9

Take a look at the PSWindowsUpdate module for PowerShell:

Install-Module -Name PSWindowsUpdate
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

0

Other way to see the solution wth monitoring EvenLogs:

function UpdateOS(){
    Write-Host "`nUpdating OS."

    # Open Eventlogs for Windows Update
    Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{Get-Content C:\Windows\SoftwareDistribution\ReportingEvents.log -Tail 1 -Wait}"

    #Define update criteria.
    $Criteria = "IsInstalled=0"

    #Search for relevant updates.
    $Searcher = New-Object -ComObject Microsoft.Update.Searcher

    $SearchResult = $Searcher.Search($Criteria).Updates

    #Download updates.
    $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()

    If ($Result.rebootRequired) { shutdown.exe /t 0 /r }
}

UpdateOS

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.