VBS script to find computer accounts in a specific OU

I currently have a script that adds a domain user as a local administrator of any computer we specify located on our domain. What I would like to have as well is a script that will cycle through a OU in AD and get a list of all the computers in that OU and then I can run my script to add a user to all the computers in the OU. LOL ok say that three times fast.

Can anyone help me with this VBS script?
LVL 1
cbielichAsked:
Who is Participating?
 
rlandquistConnect With a Mentor Commented:
Give this a try.  Modify the path to your OU on line: 11
Const ADS_SCOPE_SUBTREE = 2   
Const ForAppending = 8   
  
Set fso = CreateObject("Scripting.FileSystemObject")   
Set outFile = fso.OpenTextFile("Computers.txt", ForAppending, True)   
Set cn = CreateObject("ADODB.Connection")   
cn.Provider = "ADsDSOObject"  
cn.Open "Active Directory Provider"  
Set cmd = CreateObject("ADODB.Command")   
Set cmd.ActiveConnection = cn   
ou = "OU=3rdOU,OU=2ndOU,OU=TopOU,DC=YourDomain,DC=com"  
cmd.CommandText = "SELECT name " & _   
                  "FROM 'LDAP://" & ou & "' " & _   
                  "WHERE objectClass='computer' " & _   
                  "ORDER BY name"     
cmd.Properties("Page Size") = 1000   
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE   
Set rs = cmd.Execute   
rs.MoveFirst   
  
Do Until rs.EOF   
  WScript.Echo rs(0)   
  outFile.WriteLine rs(0)   
  rs.MoveNext   
Loop  
  
OutFile.Close   
Set outFile = Nothing  
Set fso = Nothing

Open in new window

0
 
JamesSenior Cloud Infrastructure EngineerCommented:
Please refer to the link below. This provides a couple of good vbsripts which should hopefully provide the required results.

http://www.tek-tips.com/viewthread.cfm?qid=1098379&page=2
0
 
Daz_1234Commented:
Here's another method:
'##  Change example strOU & strOutputFile variables to suit

strOU = "OU=Computers,OU=London,DC=lon-dom,DC=com"
strOutputFile = "c:\ComputerList.txt"

Set colItems = GetObject("LDAP://" & strOU)
colItems.Filter = Array("Computer")

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts  = fso.OpenTextFile(strOutputFile, 2, True)
For Each objItem In colItems
     ts.WriteLine objItem.CN
Next

ts.Close
MsgBox "Done!"

Open in new window

Hope this helps,
Daz.
0
 
cbielichAuthor Commented:
Can I get the output to loop and spit out a variable of the computer name so I can just incorporate my code for adding the account to the computer?
0
 
rlandquistCommented:
You could add your code as a function and call the function where my script echos the computer name, passing the current computer name.
the computer would have to be on, and you would have to add error checking for that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.