本文將告訴您如何自動化建立電腦帳戶。說明兩種方法:
- 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 網站:
若要使用這個方法,建立在下列範例指令碼中所述的指令碼],然後再將檔案儲存具有.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 旗標來管理使用者帳戶內容