列出最后的 Windows 更新 - vbscript

列出最后的 Windows 更新 - vbscript

我正在尝试编写一个脚本,从文本文件读取服务器列表并输出上次应用 Windows 更新的时间。

Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile ("serverlist.txt", 1)

Do Until file.AtEndOfStream
  line = file.Readline
  'wscript.echo line
  Set objSession = CreateObject("Microsoft.Update.Session", line)
  Set objSearcher = objSession.CreateUpdateSearcher
  Set colHistory = objSearcher.QueryHistory(1, 1)
  For Each objEntry in colHistory
    Wscript.Echo line
    Wscript.Echo " " & objEntry.Date
  Next
Loop

file.Close

我很困惑,因为我认为应该可以工作,但实际上不行。这是我收到的错误:

C:\Users\xxx\Desktop\new.vbs(6, 3) Microsoft VBScript 运行时错误:远程服务器计算机不存在或不可用:“CreateObject”

我是否遗漏了一些显而易见的东西?

编辑:

经过进一步调查,似乎Set objSession = CreateObject("Microsoft.Update.Session", line)无法传递变量,如果我输入“Servername”,它就可以正常工作......

答案1

敲了敲脑袋之后,我重写了脚本:

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile ("servers.csv", 1)
server = ""

Do Until file.AtEndOfStream
  line = file.Readline
  server = line
  'wscript.echo server
  Set objSession = CreateObject("Microsoft.Update.Session", server)
  If Err.Number <> 0 Then
    'WScript.Echo server & " Error: " & Err.Number & " Error (Hex): " & Hex(Err.Number) & " Source: " &  Err.Source & " Description: " &  Err.Description
    WScript.Echo server & " Communications Error"
    Err.Clear
  Else
      Set objSearcher = objSession.CreateUpdateSearcher
      Set colHistory = objSearcher.QueryHistory(1, 1)
      For Each objEntry in colHistory
        Wscript.Echo server & " " & objEntry.Date
      Next
  End If
Loop

file.Close

大多可以。显然,它在关闭的机器上会失败。但它也会在几台肯定在运行的机器上失败。那是另一天的问题。

相关内容