2012-03-16 72 views
1

我遇到了來自Microsoft站點的有關Windows Update的示例vbscript程序(名爲WUA_SearchDownloadInstall.vbs)。Windows update downloader.Download()失敗

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輸出什麼。我能怎麼做?

enter image description here

我的環境:Windows 7 32位。

[[[UPDATE]]]

我回來了這個問題。我更新了我的腳本以使用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) 
} 

剩下的問題是:如何獲得錯誤代碼下載(),這樣我可以檢查錯誤原因。 http://msdn.microsoft.com/en-us/library/windows/desktop/aa386134%28v=vs.85%29.aspx頁面似乎太粗糙,我找不到答案。

等待你的幫助。謝謝。

+0

我強烈建議你開始一個新的問題,而不是編輯這一塊。你原來的問題已經解決。您可以包含此問題的鏈接作爲背景信息。 – 2012-06-26 04:59:03

回答

3

您正在收到此錯誤,因爲Windows更新器API要求提升專家級別。在提升的命令提示符下啓動腳本應該可以解決問題。作爲便箋,您應該確保已連接到互聯網,已啓用Windows Update服務,並且沒有掛起的更新安裝(即等待在關閉時安裝)。這些東西也會導致錯誤。

[編輯]

您應該能夠從圖書館內檢索狀態。 Download方法返回status code。將其結果分配給變量可能會阻止腳本被轟炸。如果沒有,請嘗試使用On Error Goto Next來解決它。您可以在下面找到各種結果代碼和錯誤代碼。

Windows Update Agent Result Codes

WUA Networking Error Codes

+1

謝謝。你是對的。在Win7上以管理員身份運行可解決此問題。但是,如果程序提供了一種方式來告訴用戶是什麼導致了這種錯誤,那將會更好。如果不是,我們必須逐一檢查遇到的3個以上的情況中的哪一個。 – 2012-03-18 02:19:33

+0

一個正確的腳本會檢查所有三個實例以努力徹底。但是,這個特定的錯誤只是由於沒有提升priveleges而導致的。 – Nilpo 2012-03-18 17:37:00

+0

未來會有四分之一呢?應該有一種方法從庫內部獲取錯誤描述。 – 2012-03-20 03:46:15