Powershell - 安装 Windows 更新?

问题描述 投票:0回答:4

这可能吗?

我想我们需要以某种方式调用 WUAgent 来运行检测,但我想本质上下载并安装更新,然后作为脚本的一部分重新启动。

这将是一个更大脚本的一部分,基本上通过 Powershell 构建一个普通的 2008R2 盒子到 DC。

powershell powershell-2.0
4个回答
9
投票

查看 PowerShell 的 PSWindowsUpdate 模块。

它位于 这里的脚本中心


4
投票

我建议使用这个脚本

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 }

来源:https://gist.github.com/jacobludriks/9ca9ce61de251a5476f1


0
投票

通过监控查看解决方案的其他方式

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

0
投票

我尝试在远程服务器上运行脚本 UpdateOS,但总是收到错误代码 0x80070005 也被描述为“访问被拒绝”。 尝试了下面和同样的错误。

Invoke-Command -ComputerName RemoteComputerName -FilePath "C:\Path\To\Update.ps1"

使用域管理员帐户登录时也尝试了 pSSession: #服务器名称 $serverName = "服务器1"

#建立会话 $session = New-PSSession -ComputerName $serverName

#运行更新操作系统 Invoke-Command -Session $session -FilePath "C:\Path\To\Update.ps1"

© www.soinside.com 2019 - 2024. All rights reserved.