Jump to content
Sign in to follow this  

Problems with _AD_UnjoinDomain

Recommended Posts


Hi - and many thanks in advance for those who may have the answer (or can point me in the right direction)!

I'm working on a script to Unjoin a Windows 7 x64 PC from its domain (using version - there are two possible domains here, and I'd like the script to work for both, for ease of use.

The _AD_Open command works beautifully, but I always get the same return code from _AD_UnjoinDomain: "Return code '0' from Active Directory"  This makes things kinda hard to track down.

I'm not sure what I'm doing wrong here - I've tried changing the workgroup name, omitting the workgroup, etc.  I've hardcoded the user/password into the script for both domains, as they have rights to add / remove machines.  I've also tried the username in both [DOMAIN][USER] and [USER] formats.

#Include <AD.au3>


;==============[ SET VARIABLES ]==============

; [Removed for Reasons.

;============[ END SET VARIABLES ]============

; Get current domain
$ComputerDomain = StringUpper(_DomainComputerBelongs())

; Name temporary workgroup
$strWorkgroup = "DOM_TRANSFER"

; Open connection to the domain

; Leave the domain

;==============[ Open Active Directory Connection ]==============
Func _Open_Domain()

    ; Open Connection to Active Directory
    If $ComputerDomain = "[DOMAIN1].COM" Then
        $iValue = _AD_Open($strProdUser, $strProdPassword, $prodDNSDomainParam, $ComputerDomain, $prodConfigParam)
    ElseIf $ComputerDomain = "[DOMAIN2].COM" Then
        $iValue = _AD_Open($strSandUser, $strSandPassword, $sandDNSDomainParam, $ComputerDomain, $sandConfigParam)


    If @error Then Exit MsgBox(16, "Leave Domain", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

    Global $iReply = MsgBox(308, "Leave Domain", "Are you sure you want to leave the " & $ComputerDomain & " domain?")
    If $iReply <> 6 Then Exit


;==============[ Leave Current Domain ]==============
Func _Leave_Domain()

    If $ComputerDomain = "[DOMAIN1].COM" Then

        ; Leave [DOMAIN1].COM
        Global $iValue = _AD_UnJoinDomain(@ComputerName, $strWorkgroup, $strProdUser ,$strProdPassword)

    ElseIf $ComputerDomain = "[DOMAIN2].COM" Then

        ; Leave [DOMAIN2].COM
        Global $iValue = _AD_UnJoinDomain(@ComputerName, $strWorkgroup, $strSandUser ,$strSandPassword)


        MsgBox(0,"Information","No domain to unjoin.")


    ; Close Connection to Active Directory

Edited by Ragnarok
Runaway bracket removed. :)

Share this post

Link to post
Share on other sites

Solved my issue - I'd forgotten to remove the domain from the username variable.  Oops.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this