VBscript列出过去24小时内应用的补丁

时间:2014-07-17 17:20:10

标签: vbscript

我有一个VB脚本,它从文本文件,服务器列表中读取。然后,它会搜索已应用的最后一个Windows更新修补程序。我需要这个来列出过去24小时内的任何补丁。我把它传送到一个文本文件,我将在这里列出。这是我的代码:

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set objSearcher = objSession.CreateUpdateSearcher
Set file = fso.OpenTextFile ("servers.txt", 1)
server = ""
On Error Resume next

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(0, 1)
      For Each objEntry in colHistory
        Wscript.Echo server & " "
'       Wscript.Echo "Operation: " & objEntry.Operation 
'       Wscript.Echo "Result code: " & objEntry.ResultCode 
'       Wscript.Echo "Exception: " & objEntry.Exception 
        Wscript.Echo "Date: " & objEntry.Date 
        Wscript.Echo "Title: " & objEntry.Title 
'       Wscript.Echo "Description: " & objEntry.Description 
'       Wscript.Echo "Unmapped exception: " & objEntry.UnmappedException 
'       Wscript.Echo "Client application ID: " & objEntry.ClientApplicationID 
'       Wscript.Echo "Server selection: " & objEntry.ServerSelection 
'       Wscript.Echo "Service ID: " & objEntry.ServiceID 
        i = 1 
      Next
  End If
Loop

file.Close

以下是一些示例输出:

hercules 
Date: 7/10/2014 3:26:22 PM
Title: Security Update for Windows Server 2008 R2 x64 Edition (KB2971850)
ares 
Date: 7/16/2014 3:56:01 PM
Title: Security Update for Windows Server 2008 R2 x64 Edition (KB2973201)
pheme 
Date: 7/15/2014 8:04:04 PM
Title: Windows Malicious Software Removal Tool x64 - July 2014 (KB890830)

是否有其他方式而不是使用 objSearcher.QueryHistory(0, 1)?那只列出了最后一次更新。我需要它列出过去24小时内应用的所有补丁。

2 个答案:

答案 0 :(得分:0)

For Each 迭代中放置以下代码。

If DateDiff("h", objEntry.Date, Now) > 24 Then Exit Do End If

答案 1 :(得分:0)

试试这个

    On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set objSearcher = objSession.CreateUpdateSearcher
Set file = fso.OpenTextFile ("servers.txt", 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(0, 1)
    For Each objEntry In colHistory
    If DateDiff("h", objEntry.Date, Now) <= 24 Then
        Wscript.Echo server & " "
        'Wscript.Echo "Operation: " & objEntry.Operation
        'Wscript.Echo "Result code: " & objEntry.ResultCode
        'Wscript.Echo "Exception: " & objEntry.Exception
        Wscript.Echo "Date: " & objEntry.Date
        Wscript.Echo "Title: " & objEntry.Title
        'Wscript.Echo "Description: " & objEntry.Description
        'Wscript.Echo "Unmapped exception: " & objEntry.UnmappedException
        'Wscript.Echo "Client application ID: " & objEntry.ClientApplicationID
        'Wscript.Echo "Server selection: " & objEntry.ServerSelection
        'Wscript.Echo "Service ID: " & objEntry.ServiceID
    End If
  Next
  End If
Loop

file.Close