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.

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

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

' 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

End Select

Enjoy and feel free to use it as You please!

Related posts:

  1. VBScript to remove network printers, when RemovePrinterConnection fails and still have time for popcorn and a movie For the last couple of months, and some months ahead, I am working primarely on migration projects, currently...
  2. How to configure your virtual Domain Controllers and avoid simple mistakes with resulting big problems So You went ahead and used virtualized Domain Controllers for Your Active Directory domain, congratulations! I am sure...
  3. VBScript to automatically & silently remove ZenWorks regardless of version, with no hard manual labor VBScript to automatically remove ZenWorks regardless of version, with no user prompting or hard manual labor Adding to...
  4. VBScript to automatically remove a Novell client and save your feet Adding to my remote Novell migration and removal toolkit, is here below a VBScript that can be used...
  5. How to silently install ZenWorks 10 with vbScript without having to watch the screen during installation So You want to install ZenWorks 10.x.x.x silently on a machine, sounds easy enough right? Well ZenWorks is...

Leave a Reply