I come across a sample vbscript program from Microsoft site regarding Windows Update(named 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
his script runs well until it reaches
downloader.Download()
On that line, the CMD window outputs
C:\wu-install\WUA_SearchDownloadInstall.vbs(37, 1) (null): 0x80240044
By adding a printf line before downloader.Download()
, I can see that the error is asserted immediately in Download().
My question is: How can I find clue to know the error reason? May be there is way to catch the exception and let output some detailed error message.
I tried with the help of this post( Seem like a VBscript exception, how to cope with? ), and write around the problem line:
On Error Resume Next
downloader.Download()
If Err.Number <> 0 Then
WScript.Echo Err.Description
WScript.Quit 4
End If
On Error Goto 0
But WScript.Echo Err.Description
outputs nothing. How can I do?
My environment: Windows 7 32-bit.
[[[ UPDATE ]]]
I'm back on this question. I've updated my script to use JScript. Yes, it is convenient than VBScript.
Now I have such code snippet:
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)
}
Remaining question is: How do I get error code from Download() so that I can check error reason. The page at http://msdn.microsoft.com/en-us/library/windows/desktop/aa386134%28v=vs.85%29.aspx seems too coarse for me to find the answer.
Waiting for your help again. Thank you.
You are receiving this error because the Windows Updater API requires elevated priveleges. Launching your script in an elevated command prompt should solve the issue.
As a side note, you should make sure that you are connected to the internet, the Windows Update service is enabled, and that there are no pending update installations (i.e. waiting to install at shutdown). These things will also cause errors.
[edit]
You should be able to retrieve the status from inside the the library. The Download
method returns a status code. Assigning its result to a variable may prevent your script from bombing out. If not, try using On Error Goto Next
to get around it. You can find the various result codes and error codes below.
来源:https://stackoverflow.com/questions/9740694/windows-update-downloader-download-fail