Join computer (VBScript)

This code can be found in Chapter 2 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' This code joins a server to a domain.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer     = "<ComputerName>"      ' e.g. joe-xp
strDomain       = "<DomainName>"        ' e.g. rallencorp.com
strDomainUser   = "<DomainUserUPN>"     ' e.g. administrator@rallencorp.com
strDomainPasswd = "<DomainUserPasswd>"
strLocalUser    = "<ComputerAdminUser>" ' e.g. administrator
strLocalPasswd  = "<ComputerUserPasswd>"
' ------ END CONFIGURATION ---------
' Constants
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const WIN9X_UPGRADE           = 16
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144

' Connect to Computer
set objWMILocator = CreateObject("WbemScripting.SWbemLocator")
objWMILocator.Security_.AuthenticationLevel = 6
set objWMIComp = objWMILocator.ConnectServer(strComputer,  _
                                             "root\cimv2", _
                                             strLocalUser, _
                                             strLocalPasswd)
set objWMICompSys = objWMIComp.Get( _
                           "Win32_ComputerSystem.Name='" & _
                           strComputer & "'")
' Join Computer
intRC = objWMICompSys.JoinDomainOrWorkGroup(strDomain, _
                                            strDomainPasswd, _
                                            strDomainUser, _
                                            vbNullString, _
                                            JOIN_DOMAIN)
if intRC <> 0 then
    WScript.Echo "Join failed with error: " & rc
else
    WScript.Echo "Successfully joined " & strComputer & " to " & strDomain
end if

This code has been viewed 1259 times.

New from the creators of TechTasks.com: StatSheet.com