'****************************************************
'		Script Witten by Larry Heintz
'		March 2006 www.windowsadminscripts.com
' This script will enumerate all windows hot fixes
' that have been applied to the computer. This info
' will also be written to a comma delimited text file.
'
' Script Usage:
' Enum Hot Fixes on Single PC: cscript hotfix.vbs /computer:[ComputerName]
' Enum Hot Fixes on AD Domain: cscript hotfix.vbs /adwg:[AD or Work Group Name]
'****************************************************
Dim args,computername,adwg,logPath
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logPath = getLogPath()

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage:"
	wscript.echo "Enum Hot Fixes on Single PC: cscript hotfix.vbs /computer:[ComputerName]"
	wscript.echo "Enum Hot Fixes on AD Domain: cscript hotfix.vbs /adwg:[AD or Work Group Name]"
elseif args.exists("computer") then
	if computername = "" then
		computername = getComputer()
	else
		computername = computername
	end if
	Call enumHF(computername)
elseif args.exists("adwg") then
	Call enumComputers(adwg)
end if

Sub enumHF(computername)
On Error Resume Next
Dim objhf,hf
Call writetoLog("HF Description","HF Comments","HF ID","HF Installed By","HF Service Pack In Effect",computername)
set objhf = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
			("SELECT * FROM Win32_QuickFixEngineering")
	wscript.echo "Connecting to " & ucase(computername)
	if not (errorChecking (computername)) then
		for each hf in objhf
			Call writetoLog(hf.Description,hf.FixComments,hf.HotFixID,hf.InstalledBy,hf.ServicePackInEffect,computername)
		next
		wscript.echo "Retreived Hot Fix information from " & ucase(computername)
	end if
	wscript.echo "Disconnecting from " & ucase(computername) & VbCrLF
set objhf = nothing
End Sub

Function enumComputers(adwg)
On Error Resume Next
Dim objadwg,computer
Set objadwg = getobject("WinNT://" & adwg)
if not (errorChecking (adwg)) then
	objadwg.Filter = Array("Computer")
	for each computer in objadwg
		Call enumHF(computer.name)
	next
end if
Set objadwg = nothing
End Function

Function writetoLog(Description,FixComments,HotFixID,InstalledBy,ServicePackInEffect,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(logPath & "\hfinfo_" & computername & ".txt", 8,True)
	objFSOwriteline.WriteLine(Description & "," & FixComments & "," & HotFixID & "," & InstalledBy & "," & ServicePackInEffect)
	objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for ii = 0 to ubound(temp) - 1
	temp2 = temp2 & temp(ii) & "\"
next
getLogPath = temp2
End Function

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

Function errorChecking(ComputerName) 
errorChecking = False 
if err.number <> 0 then 
	wscript.echo "Unable to connect to " & ucase(ComputerName)
	Call writetoLog("Unable to connect to " & ucase(ComputerName),"","","","",computername)
	err.Clear () 
	errorChecking = True
end if 
end Function
