?
Solved

VBS script to find computer accounts in a specific OU

Posted on 2011-05-03
5
Medium Priority
?
611 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:cbielich
5 Comments
 
LVL 15

Expert Comment

by:JBond2010
ID: 35517943
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
 
LVL 12

Accepted Solution

by:
rlandquist earned 2000 total points
ID: 35522973
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
 
LVL 13

Expert Comment

by:Daz_1234
ID: 35688316
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
 
LVL 1

Author Comment

by:cbielich
ID: 35691492
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
 
LVL 12

Expert Comment

by:rlandquist
ID: 35692109
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question