如何从命令行安装 Windows 更新

如何从命令行安装 Windows 更新

是否可以从命令行安装 Windows 更新?图形更新工具在 Windows 7 中似乎不太好用。有时可以,有时不行……我的问题是,当我单击查看详细信息列表时,我看不到任何更新。因此,当有默认未选中的更新时,我无法安装它们……

那么,还有其他方法可以安装它们吗?

答案1

不确定 Windows 7 是否适用,但使用 XP/Vista,您可以运行以下命令来检测和下载更新:

wuauclt /detectnow /reportnow

如果你在使用过程中遇到问题,请查看WSUS 维基

否则,如果您有需要的特定更新,只需从 Microsoft 网站下载即可。如果出现某些问题阻止您安装,这是找出问题的最佳方法,因为 Windows 更新日志很难处理。

答案2

您可以使用脚本来同步检查、下载和安装更新。我经常使用修改版的这个vbscript用于手动修补 Windows Core 服务器。

Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
    WScript.Quit
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    addThisUpdate = false
    If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> skipping: " & update.Title & _
        " because it requires user input"
    Else
        If update.EulaAccepted = false Then
            WScript.Echo I + 1 & "> note: " & update.Title & _
            " has a license agreement that must be accepted:"
            WScript.Echo update.EulaText
            WScript.Echo "Do you accept this license agreement? (Y/N)"
            strInput = WScript.StdIn.Readline
            WScript.Echo 
            If (strInput = "Y" or strInput = "y") Then
                update.AcceptEula()
                addThisUpdate = true
            Else
                WScript.Echo I + 1 & "> skipping: " & update.Title & _
                " because the license agreement was declined"
            End If
        Else
            addThisUpdate = true
        End If
    End If
    If addThisUpdate = true Then
        WScript.Echo I + 1 & "> adding: " & update.Title 
        updatesToDownload.Add(update)
    End If
Next

If updatesToDownload.Count = 0 Then
    WScript.Echo "All applicable updates were skipped."
    WScript.Quit
End If

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        WScript.Echo I + 1 & "> " & update.Title 
        updatesToInstall.Add(update) 
        If update.InstallationBehavior.RebootBehavior > 0 Then
            rebootMayBeRequired = true
        End If
    End If
Next

If updatesToInstall.Count = 0 Then
    WScript.Echo "No updates were successfully downloaded."
    WScript.Quit
End If

If rebootMayBeRequired = true Then
    WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "Y" or strInput = "y") Then
    WScript.Echo "Installing updates..."
    Set installer = updateSession.CreateUpdateInstaller()
    installer.Updates = updatesToInstall
    Set installationResult = installer.Install()

    'Output results of install
    WScript.Echo "Installation Result: " & _
    installationResult.ResultCode 
    WScript.Echo "Reboot Required: " & _ 
    installationResult.RebootRequired & vbCRLF 
    WScript.Echo "Listing of updates installed " & _
    "and individual installation results:" 

    For I = 0 to updatesToInstall.Count - 1
        WScript.Echo I + 1 & "> " & _
        updatesToInstall.Item(i).Title & _
        ": " & installationResult.GetUpdateResult(i).ResultCode   
    Next
End If

看起来这个方法很管用,但我当然没有在 Windows 7 下测试过。如果需要,还有一个指向另一篇文章的链接,用于定位特定更新。

还有Powershell 模块揭示了类似的经历。

快速浏览后我还发现这个第三方应用程序它也使用更新 API,但具有更多选项(但要求您信任第三方代码)。

答案3

从命令行更新 Windows:

www.sysadminsoftware.com/udc.html

该工具(Updates Deployment Commander)可以满足您的要求。您还可以传递参数以避免某些补丁、针对特定更新、在完成后 N 分钟重新启动、以 CSV 格式创建报告等等。它还附带几个 GUI 实用程序。

答案4

我目前无法向回答经过达夫啤酒703,因此这里作为单独的答案:

互联网档案馆存档的 WSUSwiki 链接的最后一个“良好”版本是. 原始答案中给出的选项在此描述常见问题条目

相关内容