Using VBScript\WMI to Join a Computer to a Domain
Updated: 07 Aug 2008
Previously I had been using netdom.exe from the Server 2003 Support Tools to join computers to a domain automatically. However, using VBScript and WMI is slightly easier because it doesn't require any extra files.
The example below will create a new Computer object in the Active Directory OU that you specify. The following variables should be set according to your own environment:
strDomain - domain name
strUser - user id with rights to create a computer object
strPassword - the password for the user id specified in strUser
strOU - the Active Directory OU where the Computer object will be created, in FQDN format
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
strDomain = "DOMAIN_NAME"
strUser = "USER_NAME"
strPassword = "PASSWORD"
strOU = "ou=COMPUTERS,ou=TESTOU,dc=COMPANY,dc=COM"
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
objComputer.JoinDomainOrWorkGroup strDomain, strPassword, strDomain & "\" & strUser, strOU, JOIN_DOMAIN + ACCT_CREATE
blog entry Filed Under:

The Endpoint Management Community Blog is the perfect place to share short, timely insights including product tips, news and other information relevant to the Endpoint Management community. Any authenticated Connect member can contribute to this blog.