' hotfixchk.vbs
option explicit
' a95 /Q:Q999999
dim Computers, Computer
dim WmiSvc, QuickFixes, QuickFix, HotFixID

if WScript.Arguments.Named.Exists("Q") then
  HotFixID = WScript.Arguments.Named("Q")
else
  WScript.StdErr.WriteLine "Must provide a hotfix id as a named argument:"
  WScript.StdErr.WriteLine "   hotfixchk /Q:Q327194 or hotfixchk /Q:KB823980"
  WScript.Quit(2)
end if

if WScript.arguments.UnNamed.count = 0 then
  Computers = Array(".")
else
  set Computers = WScript.Arguments.UnNamed
end if
  WScript.StdOut.WriteLine "[" & HotFixID & "]"
  WScript.StdOut.WriteLine ";computername = hotfixinstallcount"

For Each Computer in Computers
  If Responding(Computer) then
    on error resume next
    Set WmiSvc = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")
    if err.number = 0 then
      Set QuickFixes = WmiSvc.ExecQuery _
        ("SELECT HotFixID FROM Win32_QuickFixEngineering WHERE HotFixID='" _
        & HotFixID & "'")
      WScript.StdOut.WriteLine Computer & " = " & QuickFixes.Count
    else
      WScript.StdOut.WriteLine Computer & " = WMI ERROR: 0x" & Hex(err.number)
    end if
  else
      WScript.StdOut.WriteLine Computer & " = NO RESPONSE"
  end if
Next

function Responding(byval computer)
  dim sh: set sh = CreateObject("WScript.Shell")
  dim t0: t0 = timer
  sh.Run "%comspec% /c ping -n 2 -i 3 " & computer, 0, true
  ' WScript.Echo "time to run:", computer, timer - t0
  if timer - t0 > 2 then Responding = false else Responding = true
end function
