'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This script searches AD for computernames containing the
'string defined in strSearchFor. It creates and writes to
'a text file in the current directory.
'Courtesy of monsterjta @ Tek-Tips
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
Set oRootDSE = GetObject("LDAP://rootDSE")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
strSearchFor = InputBox("Enter the string to search for:")
strDomain = "LDAP://" & oRootDSE.get("defaultNamingContext") & ""
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from '" & strDomain & "' " _
& "Where objectClass='computer'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
''''''''''''''''''''''''''
'Create and open dest file
''''''''''''''''''''''''''
strWriteFile = WSHShell.CurrentDirectory & "\" & strSearchFor & ".txt"
Set CreateFile = oFSO.CreateTextFile(strWriteFile, True)
CreateFile.Close
Set strWriteFile = oFSO.OpenTextFile(strWriteFile, ForWriting)
'''''''''''''''
'Write to file
'''''''''''''''
Do Until objRecordSet.EOF
strComputerName = objRecordSet.Fields("Name").Value
If inStr(1, strComputerName, strSearchFor, vbTextCompare) Then strWriteFile.WriteLine _
(strComputerName)
objRecordSet.MoveNext
Loop