Here are examples of VBScripts that you can use to create computer accounts. Compared with user accounts, computer
accounts are relatively easy to script because they need fewer properties. The only compulsory attributes are sAMAccountName and userAccountControl, there is no need to script sn (surname) or password. I designed these
scripts in a Windows 2003 Active Directory domain, but they should work equally well in Windows 2000.
Topics for Creating a Computer Account with VBScript
Our goal is to create computer accounts. The first mission is to create new computers in Active Directory’s Computers container and our
second mission is to create more computers, but in a named OU.
My best work is often getting people started. Therefore here on this page, we concentrate on the essential VBScript commands,
namely, to bind with Active Directory and create the Computer object. Once we have mastered the basics, I have a separate page to tackle bulk import by reading computer names from a spreadsheet.
Recommended: that you logon as administrator, preferably at a domain controller. If you are a long way from the server, Remote Desktop would be a suitable alternative.
If that is not possible, you could get these scripts to work from an XP machine as a non-administrator. However, why introduce extra complications? Especially at the beginning, you want easy success, with fewest obstacles.
Instructions for Creating a Computer Account in Active Directory
You need access to a Windows Active Directory domain.
Copy and paste the example script below into notepad or a VBScript editor.
Decide whether to change the value for strComputer.
Save the file with a .vbs extension, for example: computer.vbs.
Double click computer.vbs and check the Computers container for strComputer.
Script to Create a Computer in Your Active Directory
' Computer .vbs ' Sample VBScript to create a computer in Computers . ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.8 - May 2005 '
------------------------------------------------------' Option Explicit Dim strComputer Dim objRootLDAP, objContainer, objComputer strComputer = "XPSimple3"
' Bind to Active Directory,
Computers container. Set objRootLDAP = GetObject("LDAP://rootDSE") Set objContainer = GetObject("LDAP://cn=Computers," & _ objRootLDAP.Get("defaultNamingContext"))
' Build the actual computer.
Set objComputer = objContainer.Create("Computer",_ "cn=" & strComputer) objComputer.Put "sAMAccountName", strComputer & "$" objComputer.Put "userAccountControl", 4096 objComputer.SetInfo
Note 1: The header section (first 10 lines) explains the purpose of the script and declares the variables.
Note 2: The simple, but clever command
which allows the script to work with any domain
is: GetObject("LDAP://rootDSE"). Crucial, this statement binds WSH / VBScript to Active directory. The next line puts the focus on the Computers container, as that is where the strComputer will be
born.
Note 3: .Create is a method to build an object, observer how we use "Computer" not "User" or "OU".
Note 4: sAMAccountName controls the
ComputerName. Computer
accounts need a dollar sign appended to their name, see where I add: & "$".
Note 4: The correct userAccountControl value for a computer is 4096. (If this were a user account then
the value would be 512 or 514.)
That you already have an OU to hold the new
computers, my OU
is called Accounts
Instructions for Creating a Computer Account in a Named OU
Copy and paste the example script below into notepad or a VBScript editor.
Examine the strOU=, and if necessary, change the value of strOU to the name of your OU.
Decide whether to change the value for strComputer.
Save the file with a .vbs extension, for example: ComputerOU.vbs.
Double click ComputerOU.vbs and check the Computers container for strComputer.
' ComputerOU .vbs ' Sample VBScript to create a computer in Computers . ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.3 - May 2005 '
------------------------------------------------------' Option Explicit Dim strComputer, strOU Dim objRootLDAP, objContainer, objLeaf, objShell
' Bind to Active Directory, Computers container. Set objRootLDAP = GetObject("LDAP://rootDSE") Set objContainer = GetObject("LDAP://" & strOU & _
objRootLDAP.Get("defaultNamingContext"))
' Build the actual computer. Set objLeaf = objContainer.Create("Computer", "cn=" & strComputer) objLeaf.Put "sAMAccountName", strComputer & "$"
objLeaf.Put "userAccountControl", 4096 objLeaf.SetInfo
' Optional section to launch Active Directory Uses and Computers Set objShell=CreateObject("WScript.Shell") objShell.Run
"%systemroot%\system32\dsa.msc"
WScript.Quit
' End of Sample ComputerOU VBScript .
VBScript Tutorial - Learning Points
Note 1: The key difference between the two scripts is: strOU = "OU=Accounts ,". Trace how VBScript applies this variable to set the Organizational Unit.
Note
2: This command looks easy to script: GetObject("LDAP://" & strOU & _. However it took me ages to get the comma, the speech marks and the ampersands (&) just right. O.K there is no comma
here; because I
ended up putting the comma at the end of strOU.
Note 3: objShell.run is just me having a little fun. What this command does is open the Active Directory Users and Computers MMC ready for you to inspect
the new computer account.
In addition to creating the actual computer account, take the time to learn the VBScript object
(Computer), method (Create) and value (strComputer) technique.
Using these example scripts, you can soon create a new computer account either in an OU or in the Computers container.
Guy Thomas recommends
Computer Training Software. Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.