Function Reference

_AD_JoinDomain

Joins a computer to a domain.

#Include <AD.au3>
_AD_JoinDomain([$sComputer = @ComputerName[, $sUserParam = "", $sPasswordParam = ""[, $sOU = ""[, $iFlag = 1[, $sNewName = ""]]]]])

 

Parameters

$sComputer Optional: Name of the computer to be joined to the domain (Default = @ComputerName)
$sUserParam Optional: Domain user with admin rights to join the computer to the domain.
Supported are NetBIOSName (domain\user), user principal name (user@domain) or Windows login name (user).
(Default = credentials from _AD_Open or - if not used - of the user running the script)
$sPasswordParam Optional: Password for $sUserParam (Default = credentials from _AD_Open or - if not used - of the user running the script)
$sOU Optiona: FQDN of the OU where the computer will be placed. (Default = Keyword Default = Computers container)
$iFlag Optional: A set of bit flags (means: add the numbers for any valid combination) that specify options for joining a domain. Some of the possible flags:
1 - Joins a computer to a domain. Computer account must already exist in the domain (Default)
2 - Creates an account on a domain
32 - Allows a join to a new domain even if the computer is already joined to a domain
$sNewname Optional: Name to change the computer to after joining to the domain.

 

Return Value

Success: 1, sets @extended to the return value of the JoinDomainOrWorkgroup method:
    0 - indicates successful completion. No reboot required.
    1 - indicates successful completion with reboot required.
Failure: 0, @error set
    1 - $sComputer account does not exist in the domain and $iFlag <> 2
    2 - $sUserParam does not exist in the domain
    3 - WMI object could not be created. See @extended for error code. See remarks for further information
    4 - The computer is already a member of the domain
    5 - Joining the domain was not successful. @extended holds the Win32 error code (see: http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx)
    6 - $sNewName account already exists. You can't rename $sComputer to $sNewName
    7 - Computer could not be renamed. See @extended for the COM error code.

 

Remarks

This function allows to join the computer the script is running on or any other computer to be joined to a domain.
The domain the computer is joined to is the domain the user logged on to by using AD_Open.
If no credentials are passed to this function but have been used with _AD_Open() then the _AD_Open credentials will be used.
Using default credentials of the user running the script is only sensible when you are logged on to the domain and try to join another computer.
You have to make sure to use a valid $iFlag. The function does not check it. Most used are 1 (join) and 3 (create computer account and join).
You have to reboot the computer after a successful join to the domain.
The JoinDomainOrWorkgroup method is available only on Windows XP computer and Windows Server 2003 or later.

 

Related

_AD_CreateComputer

 

See Also

http://technet.microsoft.com/en-us/library/ee692588.aspx, http://msdn.microsoft.com/en-us/library/aa392154(VS.85).aspx

 

Example


#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
; *****************************************************************************
; Example 1
; Joins a computer to the domain - the computer account has to be created
; in advance using _AD_CreateComputer.
; *****************************************************************************
#include <AD.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

Global $iReply = MsgBox(308, "Active Directory Functions - Example 1", "This script joins a computer to the domain." & @CRLF & @CRLF & _
        "Are you sure you want to change the Active Directory?")
If $iReply <> 6 Then Exit

; Enter the computer to join
#region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Active Directory Functions - Example 1", 714, 156)
GUICtrlCreateLabel("Computer to join (NetBIOSName):", 8, 10, 231, 17)
Global $IComputer = GUICtrlCreateInput("", 241, 8, 459, 21)
Global $BOK = GUICtrlCreateButton("Join Computer", 8, 114, 121, 33)
Global $BCancel = GUICtrlCreateButton("Cancel", 628, 114, 73, 33, BitOR($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    Global $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $BCancel
            Exit
        Case $BOK
            Global $sComputer = GUICtrlRead($IComputer)
            ExitLoop
    EndSwitch
WEnd

; Join the computer to the domain
Global $iValue = _AD_JoinDomain($sComputer)
If $iValue = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Computer '" & $sComputer & "' successfully joined. Please reboot the computer")
ElseIf @error = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Computer account for '" & $sComputer & "' does not exist in the domain")
ElseIf @error = 3 Then
    MsgBox(64, "Active Directory Functions - Example 1", "WMI object could not be created. @extended=" & @extended)
ElseIf @error = 4 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Computer '" & $sComputer & "' is already a member of the domain")
ElseIf @error = 5 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Joining computer '" & $sComputer & "' to the domain was not successful. @extended=" & @extended)
Else
    MsgBox(64, "Active Directory Functions - Example 1", "Return code '" & @error & "' from Active Directory")
EndIf

; Close Connection to the Active Directory
_AD_Close()