首页 > 解决方案 > 无法使用脚本下载 Windows 更新

问题描述

我正在运行 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

标签: vbscriptwindows-update

解决方案


推荐阅读