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 1

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)

Any input would be much appreciated!
 
Post here... you should receive of find the answer you are looking for.
VBSCRIPT
forum329
 
You could do this fairly easily using LDIFDE.

Hope This Helps,

Good Luck!
 
monsterjta - I'm beginning to notice a trend here. Maybe your signature line should include "Trust me, it can be done with LDIFDE...." :)

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
Insanity1, so I needed to kill a little time and learned a new trick. Here's the code that will get what you need.

Code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'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

58sniper, I do love LDIFDE! I guess that why I put together this VBScript [dazed] Running on funes now...

Hope This Helps,

Good Luck!
 
Thanks!! Will give this a try today... have been playing with LDIFDE, but was getting more info than I really wanted...
 
As a side note, the SearchFor string is CASE SENSITIVE.

Hope This Helps,

Good Luck!
 
Just out of curiosity, is there a way to make this so that it is not case sensitive? I am seeing that some people have been adding systems in with different cases...

If not, I can run it with both cases and cat the contents to one main file...
 
Sure, here you go. I guess I was getting tired when I wrote this. I added an input box as well, so you don't need to go into the script to change the SearchFor string.

Code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'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 StrComp(strComputerName, strSearchFor, vbTextCompare) Then strWriteFile.WriteLine(strComputerName)
    objRecordSet.MoveNext
Loop

Hope This Helps,

Good Luck!
 
Thanks for all your hard work!!

FYI, your modification actually finds all systems in my test environment... =/

I may just make two renditions of your original for each system type (one searching for PC1, another for pc1) and cat the contents to one file... will be automating this process...
 
Yes, if your test environment is in the same domain then the script will pull it if the computername contains the string search. If your test environment is contained within a particular OU, then the script could be modified to bypass anything with said OU. Is this the case?
 
Sorry, my comment was not very clear =)

My test environment is a completely separate domain... what I meant to say was... if I enter something like 'MU' in the dialog box... the resulting text file contains all computer names in the domain, regardless of the search string...similar to what would happen if nothing has been entered for the search string in your original example...
 
You know, I'm not sure about that computername value. It appears not to be a string, so I cannot compare the searchfor string with it properly. I'd stick with the first script I posted, and maybe add the inputbox code to it. It will still be case sensitive, however.

Maybe someone else could chime in on the computername value not comparing to the searchfor string. I'm kinda middle-of-the-road with VBScript. Need more practice! I think I'll post it in the VBScript forum, for my own curiosity. Check back there if you're interested.

Hope This Helps,

Good Luck!
 
insanity1, here's the code for good measure. My problem was with the 'start' option in my inStr search. Apparently, this is NOT an option while doing vbTextCompare option. Learn something new every day!

Code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'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

Hope This Helps,

Good Luck!
 
Well... ran the script on a our production AD... but it didn't output all of the systems that exist... from what I can tell, it is only finding XP and 2003 systems... =/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top