Help and Support

文章编号: 315273 - 最后修改: 2007年12月1日 - 修订: 3.8

自动创建计算机帐户

本页

展开全部 | 关闭全部

概要

本文介绍如何自动创建计算机帐户。 描述两种方法:
  • netdom
  • 脚本使用 Active Directory 服务接口 (ADSI) 和 Windows Script Host 的计算机帐户

更多信息

创建计算机帐户使用"NETDOM

请注意您应使用仅在 Windows XP 版本 netdom ,附带 Windows XP CD Support\Tools\Support.cab 文件中。 以前的版本用于 Windows XP 中的所有功能无法正常工作。

您可以使用 netdom 命令行 (或从一个批处理文件 (可选) 调用该) 脚本的计算机创建帐户。 本示例将创建只有计算机帐户,并显示如何指定有权在域中创建计算机帐户的授权用户的凭据。 执行 netdom 命令的语法的以下示例
netdom 加入 ComputerName / domain: DomainName / userd: User / passwordd: UserPassword
其中,User 是有权限加入该域的用户。

有关使用 NETDOM 的详细信息,单击下面的文章编号,以查看 Microsoft 知识库中的文章:
150493? (http://support.microsoft.com/kb/150493/ ) 如何从命令行加入域

脚本使用 ADSI 和 Windows Script Host,计算机帐户

通过使用 Active Directory 服务接口 (ADSI) 和 Windows Script Host (WSH),管理员可以创建一个 Visual Basic 脚本 (VBScript) 自动创建计算机帐户。

有关 Visual Basic 脚本的详细信息,请访问下面的 Microsoft Web 站点:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx (http://msdn2.microsoft.com/en-us/library/ms950396.aspx)
若要使用此方法,下面的示例脚本所示创建脚本,然后以.vbs 扩展名中保存文件。 若要运行该文件,请双击该文件,或在命令提示符处键入 cscript myscript.vbs

示例脚本

'***********************
'* 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 = "aa312825768811d1aded00c04fd8d5cd"
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 = "TestAccount"

'*********************************************************************
'* 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 "Computers" container
'*********************************************************************

Set rootDSE = GetObject("LDAP://RootDSE")
sPath = "LDAP://<WKGUID=" & ADS_GUID_COMPUTRS_CONTAINER
sPath = sPath + ","
sPath = sPath + rootDSE.Get("defaultNamingContext")
sPath = sPath + ">"
Set computerContainer = GetObject(sPath)
sPath = "LDAP://" & computerContainer.Get("distinguishedName")
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("computer", "CN=" & sComputerName)
oComputer.Put "samAccountName", sComputerName + "$"
oComputer.Put "userAccountControl", lFlag
oComputer.SetInfo

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

sPwd = sComputerName & "$"
sPwd = LCase(sPwd)
oComputer.SetPassword sPwd

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

sUserOrGroup = "MYDOMAIN\joesmith"

'*********************************************************************
'* 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("ntSecurityDescriptor")
Set dACL = secDescriptor.DiscretionaryAcl
Set ACE = CreateObject("AccessControlEntry")

'*********************************************************************
'* An AccessMask of "-1" 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 "ntSecurityDescriptor", 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 "The command completed successfully."

'*****************
'* End Script
'*****************
Microsoft 提供仅,用于说明的编程示例不附带任何明示或默示的保证。 这包括,但不限于适销性或针对特定用途的适用性的默示保证。 本文假定您熟悉所演示的编程语言和用于创建和调试过程的该工具。 Microsoft 支持工程师可以帮助解释某个特定的过程的功能,但是它们不会修改这些示例以提供额外的功能或构建过程以满足您的特殊需求。

有关 UserAccountControl 标志的更多信息,单击下面的文章编号,以查看 Microsoft 知识库中的文章:
305144? (http://support.microsoft.com/kb/305144/ ) 如何使用 UserAccountControl 标志处理用户帐户属性

这篇文章中的信息适用于:
  • Microsoft Windows XP Home Edition
  • Microsoft Windows XP Professional Edition
  • 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
  • Microsoft Windows Server 2003, Enterprise Edition for Itanium-based Systems
  • Microsoft Windows Server 2003, Enterprise x64 Edition
  • Microsoft Windows Server 2003 Standard Edition
  • Microsoft Windows Server 2003 Web Edition
关键字:?
kbmt kbproductlink kbinfo KB315273 KbMtzh
机器翻译机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。如果您发现了错误并希望帮助我们提高机器翻译技术,请完成文章末尾的在线调查。
点击这里察看该文章的英文版: 315273? (http://support.microsoft.com/kb/315273/en-us/ )
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。

文章翻译