【问题标题】:Install windows update安装windows更新
【发布时间】:2017-01-19 10:30:39
【问题描述】:

我有以下 powershell 脚本,每次运行它时都会更新我的 Windows 操作系统。因此,我使用给定的 Windows API 来搜索、下载和安装更新。但不知何故,仅搜索它们实际上是有效的。

这是我的脚本:

$global:scriptpath = $MyInvocation.MyCommand.Path
$global:dir = Split-Path $scriptpath
$global:logfile = "$dir\updatelog.txt"

write-host " Searching for updates..."
$session = New-Object -ComObject Microsoft.Update.Session
$searcher = $session.CreateUpdateSearcher()
$result = $searcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

if ($result.Updates.Count -eq 0) {
     Write-Host "No updates to install"
} else {

    $result.Updates | Select Title

}

$downloads = New-Object -ComObject Microsoft.Update.UpdateColl

foreach ($update in $result){
    try {
        $update.AcceptEula()
        $Null = $downloads.Add($update)
    } catch {}

}

$count = $result.Updates.Count

write-host ""
write-host "There are $($count) updates available."
write-host ""

read-host "Press Enter to download\install updates"

$downloader = $session.CreateUpdateDownLoader()
$downloader.Updates = $downloads
$downloader.Download()

$installs = New-Object -ComObject Microsoft.Update.UpdateColl
foreach ($update in $result.Updates){
     if ($update.IsDownloaded){
           $installs.Add($update)
     }
}

$installer = $session.CreateUpdateInstaller()
$installer.Updates = $installs
$installresult = $installer.Install()
$installresult

但我收到以下错误:

Exception calling "Download" with "0" argument(s): "Exception from HRESULT: 0x80240024"
+ $downloader.Download()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

不知何故,这行: $downloader.Updates = $downloads 没有执行,但我不知道为什么。我也已经尝试过以管理员身份运行脚本,但也没有用。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    该错误代码是WU_E_NO_UPDATE,描述为here。基本上它说Updates 集合未设置或为空。

    【讨论】:

    • 是的,但为什么呢?它不是空的,因为我用这一行添加它们:$downloads.Add($update)。甚至遍历该列表并打印它们也可以工作
    猜你喜欢
    • 2020-03-08
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2015-07-01
    • 2018-08-22
    • 2011-10-15
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多