简体   繁体   English

Powershell - 安装 Windows 更新?

[英]Powershell - Install Windows Updates?

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.我想我们需要以某种方式调用 WUAgent 来运行检测,但我想基本上下载并安装更新,然后作为脚本的一部分重新启动。

This will be part of a larger script to basically build a vanilla 2008R2 box up to a DC all through Powershell.这将是一个更大的脚本的一部分,它基本上通过 Powershell 构建一个香草 2008R2 盒子到 DC。

Take a look at the PSWindowsUpdate module for PowerShell. 查看PowerShell的PSWindowsUpdate模块。

It is located here at the Script Center 它位于脚本中心

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 资料来源: https//gist.github.com/jacobludriks/9ca9ce61de251a5476f1

Other way to see the solution wth monitoring EvenLogs :通过监控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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM