我遇到了Microsoft网站上关于Windows Update的一个示例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
通过在downloader.Download()
之前添加printf行,我可以看到错误在Download()中立即声明。
我的问题是:如何找到线索才能知道错误原因?可能有办法捕获异常并输出一些详细的错误信息。
我在这篇文章(Seem like a VBscript exception, how to cope with?)的帮助下尝试过,并围绕问题行写一遍:
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页面似乎太粗糙了,无法找到答案。
再次等待你的帮助。谢谢。
答案 0 :(得分:3)
您收到此错误,因为Windows Updater API需要提升的权限。在提升的命令提示符下启动脚本应该可以解决问题。
作为旁注,您应该确保已连接到Internet,启用了Windows Update服务,并且没有挂起的更新安装(即等待在关机时安装)。这些事情也会导致错误。
[编辑]
您应该能够从库中检索状态。 Download
方法返回status code。将其结果分配给变量可能会阻止您的脚本被轰炸。如果没有,请尝试使用On Error Goto Next
来绕过它。您可以在下面找到各种结果代码和错误代码。