Microsoft KB Archive/315273

= Automating the creation of computer accounts =

Article ID: 315273

Article Last Modified on 12/1/2007

-

APPLIES TO


 * Microsoft Windows XP Home Edition
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional x64 Edition
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Professional Edition
 * Microsoft Windows 2000 Server
 * Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
 * Microsoft Windows Server 2003, Enterprise Edition for Itanium-based Systems
 * Microsoft Windows Server 2003, Enterprise x64 Edition
 * Microsoft Windows Server 2003, Standard Edition (32-bit x86)
 * Microsoft Windows Server 2003, Web Edition

-



This article was previously published under Q315273



SUMMARY
This article describes how to automate the creation of computer accounts. Two methods are described:
 * netdom
 * Scripting the computer account using Active Directory Service Interface (ADSI) and Windows Script Host



Creating Computer Accounts Using &quot;NETDOM&quot;
Note that you should use only the Windows XP version of netdom, which is included with the Windows XP CD in the Support\Tools\Support.cab file. Previous versions do not work correctly for all features in Windows XP.

You can use netdom from the command line (or call it optionally from a batch file) to script computer account creation. This sample creates only the computer account and displays how you can specify credentials of an authorized user who has permissions to create computer accounts in the domain. Follow this example of the syntax for the netdom command

netdom join  /domain:  /userd:  /passwordd: 

where  is a user with permission to join the domain.

For more information about using NETDOM, click the following article number to view the article in the Microsoft Knowledge Base:

150493 How to join a domain from the command line

Scripting the Computer Account Using ADSI and Windows Script Host
Using Active Directory Services Interface (ADSI) and Windows Script Host (WSH), an administrator can create a Visual Basic Script (VBScript) to automate the creation of computer accounts.

For more information about Visual Basic Scripting, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms950396.aspx

To use this method, create a script as outlined in the following sample script, and then save the file with a .vbs extension. To run the file, double-click the file or type cscript myscript.vbs at a command prompt.

Sample Script
'*********************** '* Start Script '***********************

Dim sComputerName, sUserOrGroup, sPath, computerContainer, rootDSE, lFlag Dim secDescriptor, dACL, ACE, oComputer, sPwd

'********************************************************************* '* Declare constants used in defining the default location for the '* machine account, flags to identify the object as a machine account, '* and security flags '*********************************************************************

Const UF_WORKSTATION_TRUST_ACCOUNT = &H1000 Const UF_ACCOUNTDISABLE = &H2 Const ADS_GUID_COMPUTRS_CONTAINER = &quot;aa312825768811d1aded00c04fd8d5cd&quot; Const ADS_ACETYPE_ACCESS_ALLOWED = 0 Const ADS_ACEFLAG_INHERIT_ACE = 2

'********************************************************************* '* Set the flags on this object to identify it as a machine account '* and determine the name. The name is used statically here, but may '* be determined by a command line parameter or by using an InputBox '*********************************************************************

lFlag = UF_WORKSTATION_TRUST_ACCOUNT Or UF_ACCOUNTDISABLE sComputerName = &quot;TestAccount&quot;

'********************************************************************* '* Establish a path to the container in the Active Directory where '* the machine account will be created. In this example, this will '* automatically locate a domain controller for the domain, read the '* domain name, and bind to the default &quot;Computers&quot; container '*********************************************************************

Set rootDSE = GetObject(&quot;LDAP://RootDSE&quot;) sPath = &quot;LDAP://&quot; Set computerContainer = GetObject(sPath) sPath = &quot;LDAP://&quot; & computerContainer.Get(&quot;distinguishedName&quot;) Set computerContainer = GetObject(sPath)

'********************************************************************* '* Here, the computer account is created. Certain attributes must '* have a value before calling .SetInfo to commit (write) the object '* to the Active Directory '*********************************************************************

Set oComputer = computerContainer.Create(&quot;computer&quot;, &quot;CN=&quot; & sComputerName) oComputer.Put &quot;samAccountName&quot;, sComputerName + &quot;$&quot; oComputer.Put &quot;userAccountControl&quot;, lFlag oComputer.SetInfo

'********************************************************************* '* Establish a default password for the machine account '*********************************************************************

sPwd = sComputerName & &quot;$&quot; sPwd = LCase(sPwd) oComputer.SetPassword sPwd

'********************************************************************* '* Specify which user or group may activate/join this computer to the '* domain. In this example, &quot;MYDOMAIN&quot; is the domain name and '* &quot;JoeSmith&quot; is the account being given the permission. Note that '* this is the downlevel naming convention used in this example. '*********************************************************************

sUserOrGroup = &quot;MYDOMAIN\joesmith&quot;

'********************************************************************* '* Bind to the Discretionary ACL on the newly created computer account '* and create an Access Control Entry (ACE) that gives the specified '* user or group full control on the machine account '*********************************************************************

Set secDescriptor = oComputer.Get(&quot;ntSecurityDescriptor&quot;) Set dACL = secDescriptor.DiscretionaryAcl Set ACE = CreateObject(&quot;AccessControlEntry&quot;)

'********************************************************************* '* An AccessMask of &quot;-1&quot; grants Full Control '*********************************************************************

ACE.AccessMask = -1 ACE.AceType = ADS_ACETYPE_ACCESS_ALLOWED ACE.AceFlags = ADS_ACEFLAG_INHERIT_ACE

'********************************************************************* '* Grant this control to the user or group specified earlier. '*********************************************************************

ACE.Trustee = sUserOrGroup

'********************************************************************* '* Now, add this ACE to the DACL on the machine account '*********************************************************************

dACL.AddAce ACE secDescriptor.DiscretionaryAcl = dACL

'********************************************************************* '* Commit (write) the security changes to the machine account '*********************************************************************

oComputer.Put &quot;ntSecurityDescriptor&quot;, Array(secDescriptor) oComputer.SetInfo

'********************************************************************* '* Once all parameters and permissions have been set, enable the '* account. '*********************************************************************

oComputer.AccountDisabled = False oComputer.SetInfo

'********************************************************************* '* Create an Access Control Entry (ACE) that gives the specified user '* or group full control on the machine account '*********************************************************************

wscript.echo &quot;The command completed successfully.&quot;

'***************** '* End Script '***************** Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

For more information about UserAccountControl flags, click the following article number to view the article in the Microsoft Knowledge Base:

305144 How to use the UserAccountControl flags to manipulate user account properties

Additional query words: kbadsi

Keywords: kbproductlink kbinfo KB315273

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.