1

我在 Microsoft 网站上发现了一个关于 Windows 更新的示例 vbscript 程序(名为 WUA_SearchDownloadInstall.vbs)。

http://msdn.microsoft.com/en-us/library/aa387102%28VS.85%29.aspx

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

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

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


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)
    WScript.Echo I + 1 & "> adding: " & update.Title 
    updatesToDownload.Add(update)
Next

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

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

WScript.Echo  vbCRLF & "List of downloaded updates:"

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

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

WScript.Echo  vbCRLF & _
"Creating collection of downloaded updates to install:" 

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

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

If (strInput = "N" or strInput = "n") Then 
    WScript.Quit
ElseIf (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

他的脚本运行良好,直到它到达

downloader.Download()

在该行上,CMD 窗口输出

C:\wu-install\WUA_SearchDownloadInstall.vbs(37, 1) (null): 0x80240044

通过在前面添加 printf 行downloader.Download(),我可以看到错误在 Download() 中立即断言。

我的问题是:我怎样才能找到知道错误原因的线索?可能有办法捕获异常并让输出一些详细的错误消息。

我在这篇文章的帮助下进行了尝试(看起来像一个 VBscript 异常,如何应对?),并围绕问题行写:

On Error Resume Next 
downloader.Download()
If Err.Number <> 0 Then
    WScript.Echo Err.Description
    WScript.Quit 4
End If
On Error Goto 0

WScript.Echo Err.Description什么也没输出。我能怎么做?

在此处输入图像描述

我的环境:Windows 7 32 位。

[[[ 更新 ]]]

这个问题我回来了。我已经更新了我的脚本以使用 JScript。是的,它比 VBScript 方便。

现在我有这样的代码片段:

var downloader = updsession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
try {
    downloader.Download()
}
catch(err) {
    WScript.Echo("Oops, Download error.")
    WScript.Echo("Possible reason:")
    WScript.Echo("* On Windows Vista/7, This requires you Run as Administrator.")
    WScript.Quit(3)
}

剩下的问题是:如何从 Download() 获取错误代码,以便检查错误原因。http://msdn.microsoft.com/en-us/library/windows/desktop/aa386134%28v=vs.85%29.aspx上的页面对我来说似乎太粗糙了,无法找到答案。

再次等待您的帮助。谢谢你。

4

1 回答 1

3

您收到此错误是因为 Windows Updater API 需要提升的权限。在提升的命令提示符下启动脚本应该可以解决问题。

附带说明一下,您应该确保您已连接到 Internet,Windows 更新服务已启用,并且没有挂起的更新安装(即在关机时等待安装)。这些东西也会导致错误。

[编辑]

您应该能够从库中检索状态。该Download方法返回一个状态码。将其结果分配给变量可能会阻止您的脚本被炸毁。如果没有,请尝试使用On Error Goto Next来绕过它。您可以在下面找到各种结果代码和错误代码。

Windows 更新代理结果代码

WUA 网络错误代码

于 2012-03-17T07:17:13.060 回答