Sign in to follow this  
Followers 0
Ragnarok

Problems with _AD_UnjoinDomain

2 posts in this topic

#1 ·  Posted (edited)

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 3.3.12.0) - 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>

#RequireAdmin

;==============[ 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
_Open_Domain()

; Leave the domain
_Leave_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)
    EndIf

    ;MsgBox(0,"_AD_Open",$iValue)

    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

EndFunc

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

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

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

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

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

    Else

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

    EndIf

    ; Close Connection to Active Directory
    _AD_Close()

EndFunc
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  
Followers 0