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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory Computer Name Export

Status
Not open for further replies.

insanity1

Technical User
Nov 30, 2004
40
CA
I am looking for the best way to script the export of all computer accounts in our AD Domain to text files (no other info... just computer names)...I would like to be able to query for systems with the same naming conventions and export them to their own text file (ei: all xxxxPC1 sytems to PC1.txt, all xxxxPC2 systems to PC2.txt, etc)

monsterjta provided the following script when I originally posted in the Windows 2003 Server Forum... this script works except it is only finding XP and 2003 systems in my production AD??

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'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 = "<search for string>"
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(strComputerName, strSearchFor) Then strWriteFile.WriteLine(strComputerName)
objRecordSet.MoveNext
Loop
 
Looks like that script could be adapted pretty easily to do what you want.

I don't see anything that should cause it not to find all computer objects in your current domain.

"Select Name, Location from '" & strDomain & "' " _
& "Where objectClass='computer'"

All this code does is search the current domain the all 'computer' objects in LDAP and report the Name and Location of those objects.

As long as they meet you search criteria:
strSearchFor = "<search for string>"

What I would do is add a couple of inputboxes to prompt for the search string and the output file name to quickly accomodate your need for multiple searches.

Hope this Helps.





Thanks

John Fuhrman
Titan Global Services
 
insanity1
Curious if you took a look at the last script posted on the original thread thread931-1309385



Hope This Helps,

Good Luck!

(Enamoration by Automation)
 
Hello again,

I have actually figured out what my issue was... the Wink2 systems that were not being found are in located 5 OU's deep, so for some reason were not being found... modified the script slightly and now suits my purposes:
-----------------------------------------------------------
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")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
strSearchFor = ""

objCommand.CommandText = _
"Select Name, Location from 'LDAP://ou=BLA2,ou=BLA1,dc=domain,dc=com' " _
& "Where objectCategory='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

-----------------------------------------------------------
Once again Monsterjta, your help was GREATLY appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top