文章編號: 315273 - 上次校閱: 2007年12月1日 - 版次: 3.8

自動化電腦帳戶的建立

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您如何自動化建立電腦帳戶。說明兩種方法:
  • netdom
  • 指令碼使用 Active Directory 服務介面 (ADSI) 和 Windows 指令碼主機的電腦帳戶

其他相關資訊

建立電腦帳戶使用 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 指令碼主機的電腦帳戶

使用 Active Directory 服務介面 (ADSI) 和 Windows 指令碼主機 (WSH),系統管理員可以建立 Visual Basic 指令碼 (VBScript) 來自動建立電腦帳戶。

如需有關 Visual Basic 指令碼的詳細資訊,請造訪下列 Microsoft 網站:
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
  • 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
關鍵字:?
kbmt kbproductlink kbinfo KB315273 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:315273? (http://support.microsoft.com/kb/315273/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。