Have you ever been searching for a file that you know is on one of your computers in the AD, but you fail to remember which computer the file resides on. I've had that problem on several occasions, so one day I made a vbs script that searches through all the computers in the AD for the bugger. It might take some time for this script to find your file, because it searches through the computers one by one. Maybe I'll create a new version sometime that searches for the file on several computers in parallel. The script will remember which computers it wasn't able to search, so switched off computers will be retried.

There are a number of things that could have been done better in this script. Now you can e.g. only search by exact file name, including extension (e.g. filename.exe) or substitute the file name or extension with wildcards (e.g. filename.* or *.exe). You're welcome to improve the script. If you decide to do so, please post the improved version here for everyone (including me) to enjoy.

Oh, and all scripts on this site (including this one) may be used by anyone for free, as long as you include the credits in the script code.

Well, enough babbling. Click on Read More to see the script.

'ADFileFind.vbs by Dennis Nyholm (jar@yosephine.com) 16.7.2006

On Error Resume Next

Set strDomainName = "yourdomain"
Set strDomainExt = "net"

If Wscript.Arguments.Count < 1 Then
Wscript.Echo "Too few arguments!"& Chr(10) & Chr(13) & "Usage: cscript ADfilefind.vbs "
Wscript.Quit
End If

Set ArgObj = Wscript.Arguments
ArrFilename = split(ArgObj(0), ".")

Wscript.Echo "Filename=" & ArrFilename(0)
Wscript.Echo "Extension=" & ArrFilename(1)

Const ADS_SCOPE_SUBTREE = 2
Const MBCONVERSION= 1048576
Const ForReading = 1
Const ForWriting = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, Location from 'LDAP://DC=" & strDomainName & ",DC=" & strDomainExt & "' Where objectClass='computer'"
objCommand.Properties("Page Size") = 2000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Err.Clear

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFile = FSO.CreateTextFile("Workfile.txt", True)
objFile.Close
Set objFile = FSO.OpenTextFile("Workfile.txt", ForWriting)


Do Until objRecordSet.EOF

objFile.WriteLine objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop
objFile.Close

Do
strCompsLeft = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("Workfile.txt", ForReading)

Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile2 = objFSO2.CreateTextFile("Workfile2.txt", True)
objTextFile2.Close

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
Wscript.Echo Chr(10) & Chr(13) & strComputer & ": "
If ArrFilename(0) = "*" Then
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where Extension = '" & ArrFilename(1) & "'")
ElseIf ArrFilename(1) = "*" Then
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where FileName = '" & ArrFilename(0) & "'")
Else
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where FileName = '" & ArrFilename(0) & "' and Extension = '" & ArrFilename(1) & "'")
End If
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Else
Set objTextFile2 = objFSO2.OpenTextFile("Workfile2.txt", ForWriting)
objTextFile2.WriteLine strComputer
obj.TextFile2.Close
Err.Clear
strCompsLeft = strCompsLeft + 1
End If
Loop
objTextFile.Close

objFSO.DeleteFile("Workfile.txt")

Set objFile = FSO.CreateTextFile("Workfile.txt", True)
objFile.Close
Set objFile = FSO.OpenTextFile("Workfile.txt", ForWriting)
Set objTextFile2 = objFSO2.OpenTextFile("Workfile2.txt", ForReading)
Do Until objTextFile2.AtEndOfStream
strTextLine = objTextFile2.Readline
objFile.WriteLine strTextLine
Loop
objFile.Close
objTextFile2.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("Workfile2.txt")
Wscript.Echo "Computers left: " & strCompsLeft
Loop