无法使用脚本下载Windows更新

时间:2018-05-21 07:25:07

标签: vbscript windows-update

我正在运行Windows 2015 LTSB并使用以下脚本在需要时下载Windows修补程序。 以前,下面的脚本用于普通用户,但过去几天它只显示消息

  

下载已成功完成。请按任意键继续。

实际上没有下载任何内容,此脚本不会安装更新。 当我从具有管理员权限的用户执行相同的脚本时,它会下载并安装相关的更新包。

我怀疑downloader.Download()对于管理员和普通用户的行为有所不同。

请您帮助我了解更新的原因是什么,而不是从普通用户那里下载。

Option Explicit
Dim updateSession, updateSearcher, update, searchResult, downloader, updatesToDownload, updatesToInstall, installer, installationResult, InputKey, i, endKey

If Right((LCase(WScript.FullName)),11) <> "cscript.exe" Then
    WScript.Echo "Please carry out this script using CSCRIPT.EXE." & _
        vbCrLf & "Example: cscript WindowsUpdate.vbs"
    WScript.StdOut.Write vbCrLf & "Please press any key to continue."
    endKey = WScript.StdIn.ReadLine
    WScript.Quit(0)
End If
Dim strInp
On Error Resume Next
WScript.Echo "Press Enter key to begin Windows Update."
strInp = WScript.StdIn.ReadLine

WScript.Echo "------------------------------"
WScript.Echo "Windows Update"
WScript.Echo "------------------------------"
WScript.Echo "Verifying latest update..."
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set searchResult = _
    updateSearcher.Search("IsInstalled=0 and Type='Software' and AutoSelectOnWebSites=1")
'If Err.Number <> 0 Then
If IsNull(searchResult.Updates.Count) Or IsEmpty(searchResult.Updates.Count) Then
    WScript.Echo "An error occurred. Please check your network connection and try again."
    WScript.StdOut.Write vbCrLf & "Please press any key to continue."
    endKey = WScript.StdIn.ReadLine
    'Err.Clear
    WScript.Quit(0)
End If

For i = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(i)
    WScript.Echo i + 1 & vbTab & update.Title & " Size: " & update.MaxDownloadSize
Next

If searchResult.Updates.Count = 0 Then
    WScript.Echo "Windows is up to date."
    WScript.StdOut.Write vbCrLf & "Please press any key to continue."
    endKey = WScript.StdIn.ReadLine
    WScript.Quit(0)
Else
    WScript.Echo "A newer version of " & searchResult.Updates.Count & _
        " is found. Downloading starts."
End If

WScript.StdOut.Write "Preparing download..."
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For i = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(i)
    WScript.StdOut.Write "."
    updatesToDownload.Add(update)
Next

WScript.Echo vbCrLf & "Newer version of programs is downloading..."
Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()
'WScript.Echo "DEBUG [downloader.Download().ResultCode]:" & downloader.Download().ResultCode

If downloader.Download().ResultCode = 2 Then
    WScript.Echo "Download completed successfully."
Else
    WScript.Echo "Download failed."
End If
If downloader.Download().ResultCode = 4 Then
    WScript.StdOut.Write vbCrLf & "Please press any key to continue."
    endKey = WScript.StdIn.ReadLine
    WScript.Quit(0)
End If

WScript.Echo "Download the following programs is successfully completed."
For i = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(i)
    If update.IsDownloaded Then
        WScript.Echo i + 1 & vbTab & update.Title
    End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
WScript.StdOut.Write "Preparing installation..." 
For i = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(i)
    If update.IsDownloaded = True Then
        WScript.StdOut.Write "."
        updatesToInstall.Add(update) 
    End If
Next

'WScript.StdOut.Write vbCrLf & "DEBUG [updatesToInstall.Count]:" & updatesToInstall.Count
If updatesToInstall.Count = 0 Then
    WScript.Echo vbCrLf & "Installation failed."
    WScript.StdOut.Write vbCrLf & "Please press any key to continue."
    endKey = WScript.StdIn.ReadLine
    WScript.Quit(0)
End If

WScript.Echo vbCrLf & "Installing..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()

If installationResult.ResultCode = 2 Then
    WScript.Echo "Installation completed successfully."
Else
    WScript.Echo "Installation failed."
End If
WScript.Echo "Detail information."
For i = 0 to updatesToInstall.Count - 1
    WScript.StdOut.Write i + 1 & vbTab & _
        updatesToInstall.Item(i).Title
    If installationResult.GetUpdateResult(i).ResultCode = 2 then
        WScript.Echo "Succeed."
    Else
        WScript.Echo "Failure."
    End If
Next

'WScript.StdOut.Write "Restart is required."
If installationResult.RebootRequired Then
    'WScript.Echo "Required."
    WScript.Echo "Computer restart is required to complete installation."
Else
    WScript.Echo "Computer restart is not required."
End If

WScript.StdOut.Write vbCrLf & "Please press any key to continue."
InputKey = WScript.StdIn.ReadLine
If installationResult.RebootRequired Then
    Dim objWshShell
    Set objWshShell = WScript.CreateObject("WScript.Shell")
    objWshShell.Run "%comspec% /c shutdown /r /t 0", 0, False
    WScript.Quit(-1)
Else
    WScript.Quit(0)
End If

0 个答案:

没有答案