Join Domain Script + RRS feed

  • Question

  • Hi I got this script from another helpful forum post

    I was wondering if someone could add to it so if it comes back as CASE 0 then the machine restarts Cheers

    ' JoinDomain.vbs
    ' VBScript program to join a computer to a domain.
    ' The computer account is created in Active Directory.
    ' The computer must have XP or above.
    ' The AD must be W2k3 or above.
    ' See c:\Windows\debug\NetSetup.log for details.

    Option Explicit

    Dim strDomain, strUser, strPassword, strOU
    Dim objNetwork, strComputer, objComputer, lngReturnValue

    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const JOIN_UNSECURE = 64
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144

    ' Prompt for credentials.
    strDomain = InputBox("Enter name of domain")
    strUser = InputBox("Enter account name to use")
    strPassword = InputBox("Enter the account password")

    ' Specify the OU where the computer object will be created.
    strOU = "ou=Computers,ou=West,dc=mydomain,dc=com"

    ' Retrieve NetBIOS name of local computer.
    Set objNetwork = CreateObject("WScript.Network")
    strComputer = objNetwork.ComputerName

    Set objComputer = GetObject("winmgmts:" _
        & "{impersonationLevel=Impersonate,authenticationLevel=Pkt}!\\" & _
        strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
            strComputer & "'")

    lngReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
        strPassword, strDomain & "\" & strUser, strOU, _

    Wscript.Echo "ReturnValue = " & CStr(lngReturnValue)

    Select Case lngReturnValue
        ' Some return code values (added 04/05/2010).
        Case 0
            Wscript.Echo "Success joining computer to the domain!"
        Case 5
            Wscript.Echo "Access is denied"
        Case 87
            Wscript.Echo "The parameter is incorrect"
        Case 110
            Wscript.Echo "The system cannot open the specified object"
        Case 1323
            Wscript.Echo "Unable to update the password"
        Case 1326
            Wscript.Echo "Logon failure: unknown username or bad password"
        Case 1355
            Wscript.Echo "The specified domain either does not exist or could not be contacted"
        Case 2224
            Wscript.Echo "The account already exists"
        Case 2691
            Wscript.Echo "The machine is already joined to the domain"
        Case 2692
            Wscript.Echo "The machine is not currently joined to a domain"
        Case Else
            Wscript.Echo "Unknown error"
    End Select


    • Edited by GWynne Monday, February 3, 2014 2:09 PM Title change
    Monday, February 3, 2014 2:06 PM


All replies

  • Sure, it's possible. But first, have you tried to make the modification yourself?

    If so, what modifications do you think should be made?

    If not, why not?


    Monday, February 3, 2014 3:25 PM
  • Because I have basic understanding of scripting so I wanted help from people who do???

    Monday, February 3, 2014 4:33 PM
  • Because I have basic understanding of scripting so I wanted help from people who do???

    What you are asking makes little sense.  "Case 0" is what is returned from the Join operation.  This cannot be altered to come back as you ask.

    I suggest starting by learning how a Windows system is joined to a domain and what WMI actually does.

    The best suggestion is to learn how to script and to write code that pings the machine until it comes back from the restart.

    You can also use PowerShell V3 and later to do a join with wait.

    add-computer -domainname <domain> -computername <name> -Credential domain\userid
    restart-computer -ComputerName <name> -Wait

    You can also look in the Repository for scripts that already do what you are asking.


    Monday, February 3, 2014 4:50 PM
  • Here is a very simple example of a VBScript Select Case statement:

    Dim lngReturnValue
    lngReturnValue = 0
    Select Case lngReturnValue
      Case 0
        WScript.Echo "lngReturnValue is 0"
      Case 1
        WScript.Echo "lngReturnValue is 1"
    End Select

    If you copy and paste this to a new .vbs file and run it, what will be the output?

    Also, it might be helpful to take a look at the documentation:


    Monday, February 3, 2014 6:33 PM
  • This exact question for this exact script has been answered in this exact forum nearly exactly one year ago today. :D

    See Add restart command on vbscrpit

    Monday, February 3, 2014 6:44 PM
  • Thanks Jason that works great

    Thursday, February 13, 2014 12:10 PM