VBScript to join computers to domain, with specific user and avoid having to manually place them in AD

The following script was used for automatically joining alot of computers to an Active Directory domain, it was required to place the computer in a specific Organizational Unit and also to run with a specified user with only permissions to add machines in this OU and the default new computers OU (giving it unlimited join domain permissions).

So here is a cleaned up short script to join a machine to a domain, using a script specified user (could be changed easily to current user) and place the machine in a specific OU, great for running for specific departments, so You avoid having to manually sort the machines in the end.

Get the txt file with the script here.

On Error Resume Next
' This script joins the current computer to a domain, using specified user and placing it in specified OU
' Created by Sole Viktor - sole@sole.dk

' Set theese variables
strDomain = "mydomain.local" ' Domain to logon
strPassword = "MyPassword" ' Service account logon password
strUser = "MyUserAccount" ' Service account
strOU = "OU=LetsPlaceItHere,OU=MySecondOU,OU=MyFirstOU,DC=mydomain,DC=local" ' OU to place computer in

' Constants to choose from when joining
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

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

' Join Domain
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "\" & strUser, strOU, _
JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)

Select Case ReturnValue

Case 0 Status = "Success"

Case 2 Status = "Missing OU"

Case 5 Status = "Access denied"

Case 53 Status = "Network path not found"

Case 87 Status = "Parameter incorrect"

Case 1326 Status = "Logon failure, user or pass"

Case 1355 Status = "Domain can not be contacted"

Case 1909 Status = "User account locked out"

Case 2224 Status = "Computer Account allready exists"

Case 2691 Status = "Allready joined"

Case Else Status = "UNKNOWN ERROR " & ReturnValue

' Show Status
WScript.Echo "Join domain status: " & Status

End Select

Enjoy and feel free to use it as You please!

55 Responses to “VBScript to join computers to domain, with specific user and avoid having to manually place them in AD”

  • Sole:

    with quotes

  • Kenyatta Runnels:

    Trying to run your script with modifications that reflects my organization. I continue to receive a script error.

    Line 1
    Character 1
    Invalid Character

    Can you help me out with this. Thxs

  • Claus:

    @Kenyatta Runnels
    Try copy the script to Notepad and be sure to save it as “ANSI”. Hope it helps.

  • Vlad:

    So how do I run it? Do I run it as logon script when end user logs in. Or do I log in each computer and run this script on command prompt? How does it know the list of computers I want to join to the domain? -I don’t see an option for list of netbios names to be joined. Thank you.

  • Luis:

    Have anybody see ” Join Domain Status: UNKNOWN ERROR 2693″, when trying to use this script?

    Thanks

Leave a Reply