搜索、下载和安装特定更新

本主题中的脚本示例演示如何使用 Windows 更新代理 (WUA) 扫描、下载和安装特定更新。 更新可以通过其标题指定。

该示例搜索特定的软件更新,下载更新,然后安装更新。 例如,用户可以使用此方法来确定计算机上是否安装了关键安全更新程序。 如果未安装更新,则用户可以确保下载并安装更新。 用户还可以确保收到有关安装状态的通知。

示例更新由 IUpdate 的 Title 属性中的更新标题标识。 此示例中建议的更新标题为“适用于 Windows Rights Management 客户端 1.0 的更新”。

注意

有关如何搜索、下载和安装应用于特定应用程序的所有更新的信息,请参阅搜索、下载和安装更新

 

在尝试运行此示例之前,请注意以下事项:

  • 必须在计算机上安装 WUA。 有关如何确定已安装的 WUA 版本的详细信息,请参阅确定 WUA 的当前版本
  • 该示例没有提供自己的用户界面。 如果更新需要重启,则 WUA 会提示用户重启计算机。
  • 该示例只能从 WUA 下载更新。 它无法从软件更新服务 (SUS) 1.0 服务器下载更新。
  • 运行此示例需要 Windows 脚本宿主 (WSH)。 有关 WSH 的详细信息,请参阅平台软件开发工具包 (SDK) 的 WSH 部分。 如果示例复制到名为 WUA_SpecificUpdate.vbs 的文件,则可以通过打开命令提示符窗口并键入以下命令来运行它:cscript WUA_SpecificUpdate.vbs

注意

执行扫描时,可能会遇到高于正常内存使用量的情况。 建议对系统进行必要的调整,以便为扫描过程分配足够的内存资源。 这可能包括配置其他处理器以及修改页面文件。 确保分配足够的内存将有助于高效、有效地完成扫描。

示例

重要

此脚本旨在演示如何使用 Windows 更新代理 API,并提供开发人员如何使用这些 API 解决问题的示例。 此脚本不打算作为生产代码,Microsoft 也不支持脚本本身(尽管支持基础 Windows 更新代理 API)。

 

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

'Get update title to search for
WScript.Echo "Enter the title of the update: " & _
"(for example, Update for Windows Rights Management client 1.0)"
updateTitle = WScript.StdIn.Readline

WScript.Echo vbCRLF & "Searching for: " & updateTitle & "..."

Set updateSearcher = updateSession.CreateupdateSearcher()

'Search for all software updates, already installed and not installed
Set searchResult = _
updateSearcher.Search("Type='Software'")

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

updateIsApplicable = False

'Cycle through search results to look for the update title
For i = 0 To searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(i)
   If UCase(update.Title) = UCase(updateTitle) Then
   'Update in list of applicable updates 
   'Determine if it is already installed or not
      If update.IsInstalled = False Then
         WScript.Echo vbCRLF & _
         "Result: Update applicable, not installed."
         updateIsApplicable = True
         updateToInstall.Add(update)
      Else 
         'Update is installed so notify user and quit
         WScript.Echo vbCRLF & _
         "Result: Update applicable, already installed."
         updateIsApplicable = True
         WScript.Quit 
      End If
   End If 
Next

If updateIsApplicable = False Then
   WScript.Echo "Result: Update is not applicable to this machine."
   WScript.Quit
End If

WScript.Echo vbCRLF & "Would you like to install now? (Y/N)"
stdInput = WScript.StdIn.Readline
 
If (strInput = "N" or strInput = "n") Then 
   WScript.Quit
ElseIf  (stdInput = "Y" OR stdInput = "y") Then
   'Download update
   Set downloader = updateSession.CreateUpdateDownloader() 
   downloader.Updates = updateToInstall
   WScript.Echo vbCRLF & "Downloading..."
   Set downloadResult = downloader.Download()
   WScript.Echo "Download Result: " & downloadResult.ResultCode
  
   'Install Update
   Set installer = updateSession.CreateUpdateInstaller()
   WScript.Echo vbCRLF & "Installing..."
   installer.Updates = updateToInstall
   Set installationResult = installer.Install()
  
   'Output the result of the installation
   WScript.Echo "Installation Result: " & _
   installationResult.ResultCode
   WScript.Echo "Reboot Required: " & _
   installationResult.RebootRequired 
End If