【问题标题】:VBS Script for Windows Update用于 Windows 更新的 VBS 脚本
【发布时间】:2015-06-27 17:00:19
【问题描述】:

如何删除用户请求“批准”此 .VBS 脚本中的所有内容,而让它自动运行/安装找到的所有内容?

脚本如何运行的详细信息:

此 .VBS 脚本在 Windows 计算机上运行,​​搜索 Windows 更新,然后手动要求用户“确定”它找到的每个更新。一旦用户点击“确定”并接受找到的更新,它就会下载它。

一旦 Widnows 更新下载,它会再次要求用户批准每个 Windows 更新安装。这不是自动化的.....我对 .VBS 的熟悉程度不足以编辑此脚本。

Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

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)
    addThisUpdate = false
    If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> skipping: " & update.Title & _
        " because it requires user input"
    Else
        If update.EulaAccepted = false Then
            WScript.Echo I + 1 & "> note: " & update.Title & _
            " has a license agreement that must be accepted:"
            WScript.Echo update.EulaText
            WScript.Echo "Do you accept this license agreement? (Y/N)"
            strInput = WScript.StdIn.Readline
            WScript.Echo 
            If (strInput = "Y" or strInput = "y") Then
                update.AcceptEula()
                addThisUpdate = true
            Else
                WScript.Echo I + 1 & "> skipping: " & update.Title & _
                " because the license agreement was declined"
            End If
        Else
            addThisUpdate = true
        End If
    End If
    If addThisUpdate = true Then
        WScript.Echo I + 1 & "> adding: " & update.Title 
        updatesToDownload.Add(update)
    End If
Next

If updatesToDownload.Count = 0 Then
    WScript.Echo "All applicable updates were skipped."
    WScript.Quit
End If

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        WScript.Echo I + 1 & "> " & update.Title 
        updatesToInstall.Add(update) 
        If update.InstallationBehavior.RebootBehavior > 0 Then
            rebootMayBeRequired = true
        End If
    End If
Next

If updatesToInstall.Count = 0 Then
    WScript.Echo "No updates were successfully downloaded."
    WScript.Quit
End If

If rebootMayBeRequired = true Then
    WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (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

【问题讨论】:

  • 您的意思是在没有回显消息的情况下运行它吗?我的意思是它不与用户交互?
  • @Hackoo 现在这个脚本确实与用户交互。它要求用户“批准”每个更新。这导致 30 个弹出窗口,我试图删除用户交互并让它在未经批准的情况下运行。只需自动批准找到的所有内容并安装即可。感谢您检查它!
  • @Hackoo 我误解了你的问题。我希望它在没有回声消息的情况下运行。你是对的。它确实交互,虽然我不希望它交互。
  • 你应该在这种情况下 cmets 每行以 wscript.echo 开头

标签: windows vbscript windows-update


【解决方案1】:
  1. 脚本输出通过WScript.Echo方法:run your script using the command-line-based script host(例如Cscript.exe YourScript.vbs)。
  2. 用户输入:将 strInput = WScript.StdIn.Readline 替换为 strInput = "Y"(脚本中出现的所有内容)。
  3. 保留更新日志:使用Cscript.exe YourScript.vbs > YourLog.txt

解释:

  1. 无论您使用WScript 还是CScript,您仍然以相同的方式运行脚本。区别仅在于输出——WScript 生成窗口输出,而CScript 将其输出发送到启动它的命令窗口。初始安装时,默认主机为WScript。要将其更改为 CScript,请在命令行中键入以下内容:cscript //h:cscript
  2. 我不会使用Cscript.exe YourScript.vbs < Prepared-Y.txt 重定向:我们不知道输入文件中Y 的行数,因为我们无法提前估计更新的数量;可能导致错误Microsoft VBScript runtime error: Input past end of file
  3. 阅读Redirection

【讨论】:

  • 这工作正常,我使用 WScript 进行输出。感谢您的辛勤工作!
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多