Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to check all computers for a particular file

Status
Not open for further replies.

jlotz1

Technical User
Feb 1, 2010
6
US
I have been tasked with writing a vbscript to help audit our company PC's. The script will be ran on-demand, and when ran it will query AD for all domain computers, ping each one, and if it can ping it, it will then check for the existance of a particular .exe file (in our case, an antivirus program .exe). It should put the results (found/not found) into a text or .csv file.

I'm not sure of the best way to do this. I'm also not sure how to handle instances where the program may be installed in a different location, for example d:\program files instead of c:\program files.

Any help getting me started would be appreciated. Thanks in advance.
 
[red] 1. Query ADO for ALL computer accounts[/red]
[blue] 2. Cycle through the collection[/blue]
[green] 3. If we can get the computers WMI, it is on and accessible [/green]
[purple] 4. Collect the computer's local disks and check for the file[/purple]
Output

Code:
'************************************************************************
' VARIABLE DEFINITION
'************************************************************************

strFilePath = "\Program Files\Anti Virus\av.exe"

[red]
set objArgs  = Wscript.Arguments
set objCommand = CreateObject("ADODB.Command")
set objConnection = CreateObject("ADODB.Connection")
set objFSO   = WScript.CreateObject("Scripting.FileSystemObject")
set objRootDSE = GetObject("LDAP://RootDSE")
set objShell = WScript.CreateObject("WScript.Shell")
[/red]
'************************************************************************
' BEGIN
'************************************************************************
[COLOR=#008080]
set objOutput = objFSO.OpenTextFile("c:\temp\results.csv", 2, true, 0)
objOutput.WriteLine "Computer,Status"
[/color]
[red]
strDNSDomain = objRootDSE.Get("defaultNamingContext")	
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider" 
objCommand.ActiveConnection = objConnection
objCommand.Properties("Timeout") = 30
objCommand.Properties("Page Size") = 100
objCommand.Properties("Cache Results") = False
objCommand.CommandText = "SELECT sAMAccountName FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory='computer'"
set objRecordSet = objCommand.Execute
[/red]
[blue]
do until objRecordSet.EOF
	strComputer = objRecordset.Fields("sAMAccountName").Value
	strComputer = left(strComputer, len(strComputer) - 1)
[green]
	on error resume next
	set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

	if (Err.number = 0) then
		on error goto 0
[purple]		set colDisks = objWMI.ExecQuery("Select * From Win32_LogicalDisk")
		boolFileExists = false
		for each objDisk in colDisks
			strPath = objDisk.Name & strFilePath
			if (objFSO.FileExists(strPath)) then
				objOutput.WriteLine strComputer & "," & strPath
				boolFileExists = true
			end if
		next
[/purple]		if (boolFileExists = false) then
[COLOR=#008080]			objOutput.WriteLine strComputer & ",File Does Not Exist"[/color]
		end if
	else
[COLOR=#008080]		objOutput.WriteLine strComputer & ",Computer not accessible"[/color]
	end if
[/green]	objRecordSet.MoveNext
loop
[/blue]
[COLOR=#008080]
objOutput.close
[/color]
msgbox "done"

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
One thing I would suggest to modify the excellentscript Geates has provided would be to query the OS for bit type (Win32_OperatingSystem OSArchitecture), that way you will know if you need to be looking in Programs Files or Program Files (x86).

You can check it like this:

Code:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
    If objItem.OSArchitecture = "32-bit" Then
    	ProgramDir = "Program Files"
    Else
    	ProgramDir = "Program Files (x86)"
    End If
Next

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top