Sign in to follow this  
Followers 0
skysel

Joining domain...

39 posts in this topic

#1 ·  Posted (edited)

I still have no idea why script below doesn't join computer to domain... It just exits with return code 0... Rename works fine.. Been strugling with this issue for whole week now ;(

;Constants

Const $JOIN_DOMAIN = 1
Const $ACCT_CREATE = 2
Dim $sCmpName
Dim $sUser, $sPassword, $sDomain, $sOU

;User / Domain Data

$sUser = "deploy"
$sPassword = "xxxxx"
$sDomain = "DOMAIN"
$sOU = "<ou=Computers,dc=DOMAIN,dc=com>";<ou=myou,dc=mydomain,dc=com>

;Input new Computer Name

$sCmpName = InputBox("Enter the new computer name:", "Computer Name")
If $sCmpName = "" Then
    MsgBox(0,"Fail","Exiting script.")
    Exit
EndIf

Dim $oWMI, $oCmp, $oOS, $sReturn

;Rename Computer

$oWMI = ObjGet("winmgmts:\\.\root\cimv2")
    
For $oCmp in $oWMI.InstancesOf("Win32_ComputerSystem")
    $sReturn = $oCmp.Rename($sCmpName)
    If $sReturn <> 0 Then
        MsgBox(0,"Fail","Rename failed.  Error = " & @error & @CRLF & "Exiting script.")
        Exit
    Else
        MsgBox(0,"Rename","Rename successful.")
;Join to Domain

$ReturnValue = $oCmp.JoinDomainOrWorkGroup($sDomain, $sPassword, $sDomain & "\" & $sUser, $sOU, $JOIN_DOMAIN+$ACCT_CREATE)
        If $ReturnValue <> 0 Then
            MsgBox(0,"Fail","Join domain failed.  Error = " & @error & @CRLF & "Exiting script.")
            Exit
        Else
            MsgBox(0,"Success","Join domain successful.")
            MsgBox(0,"Reboot","Rebooting computer...")
            RunWait("shutdown -r -t 00")
        EndIf
    EndIf
Next
Edited by skysel

Share this post


Link to post
Share on other sites



I've not done it myself but adfunctions.au3 offers a function to create a computer in a specific OU (_ADCreateComputer).

adfunctions.au3 can be found at http://www.autoitscript.com/forum/index.ph...amp;showfile=66


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

otherwise to see what the error is... hope this Error Handler works as I don't have the possibility to test :)

;Constants

Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler

Const $JOIN_DOMAIN = 1
Const $ACCT_CREATE = 2
Dim $sCmpName
Dim $sUser, $sPassword, $sDomain, $sOU

;User / Domain Data

$sUser = "deploy"
$sPassword = "xxxxx"
$sDomain = "DOMAIN"
$sOU = "<ou=Computers,dc=DOMAIN,dc=com>";<ou=myou,dc=mydomain,dc=com>

;Input new Computer Name

$sCmpName = InputBox("Enter the new computer name:", "Computer Name")
If $sCmpName = "" Then
    MsgBox(0, "Fail", "Exiting script.")
    Exit
EndIf

Dim $oWMI, $oCmp, $oOS, $sReturn

;Rename Computer

$oWMI = ObjGet("winmgmts:\\.\root\cimv2")

For $oCmp In $oWMI.InstancesOf("Win32_ComputerSystem")
    $sReturn = $oCmp.Rename($sCmpName)
    If $sReturn <> 0 Then
        MsgBox(0, "Fail", "Rename failed.  Error = " & @error & @CRLF & "Exiting script.")
    Else
        MsgBox(0, "Rename", "Rename successful.")
        ;Join to Domain
        $ReturnValue = $oCmp.JoinDomainOrWorkGroup($sDomain, $sPassword, $sDomain & "\" & $sUser, $sOU, $JOIN_DOMAIN + $ACCT_CREATE)
        If $ReturnValue <> 0 Then
            MsgBox(0, "Fail", "Join domain failed.  Error = " & @error & @CRLF & "Exiting script.")
        Else
            MsgBox(0, "Success", "Join domain successful.")
            MsgBox(0, "Reboot", "Rebooting computer...")
            RunWait("shutdown -r -t 00")
        EndIf
    EndIf
Next

If $g_eventerror Then
    $g_eventerror = 0
    MsgBox(0, "AutoItCOM test", "Test passed: We got an error number: " & @error)
Else
    MsgBox(0, "AutoItCOM test", "Test failed!")
EndIf

Exit

; This is my custom defined error handler
Func MyErrFunc()

    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
            "err.description is: " & @TAB & $oMyError.description & @CRLF & _
            "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
            "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
            "err.source is: " & @TAB & $oMyError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
            )

    Local $err = $oMyError.number
    If $err = 0 Then $err = -1

    $g_eventerror = $err ; to check for after this function returns
EndFunc   ;==>MyErrFunc

Share this post


Link to post
Share on other sites

KaFu:

First dialog is:

"Join domain failed. Error = 0"

Second is:

"AutoItCom test: Test failed!"

Share this post


Link to post
Share on other sites

Hmmm, opposite to what might be assumed, the second msgbox means that it does not seem to be a COM error. Maybe recheck the MSDN documentation for the JoinDomainOrWorkgroup method.

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

What catched my eye is the following:

Example: "OU=testOU, DC=domain, DC=Domain, DC=com"

Another source for error might be the flag 2 which means it creates the account. Does the account already exist in the Domain? Many ways to proceed :).

Share this post


Link to post
Share on other sites

Hmmm, opposite to what might be assumed, the second msgbox means that it does not seem to be a COM error. Maybe recheck the MSDN documentation for the JoinDomainOrWorkgroup method.

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

What catched my eye is the following:

Example: "OU=testOU, DC=domain, DC=Domain, DC=com"

Another source for error might be the flag 2 which means it creates the account. Does the account already exist in the Domain? Many ways to proceed :).

I have tried just about any combination today.. this is really driving me nuts. WHY THE HELL WON'T IT WORK?? :)

Share this post


Link to post
Share on other sites

You can try this command to see if it works, I've tested this command myself and I know it works.

If this fails aswell it has nothing to do with your script method:

RunWait (@Comspec & ' /k ' & $netdom & ' join . /Domain:DOMAIN.COM /UserD:deploy /PasswordD:xxxx ')

Cheers,

Share this post


Link to post
Share on other sites

You can try this command to see if it works, I've tested this command myself and I know it works.

If this fails aswell it has nothing to do with your script method:

RunWait (@Comspec & ' /k ' & $netdom & ' join . /Domain:DOMAIN.COM /UserD:deploy /PasswordD:xxxx ')

Cheers,

Thanks for the input, but I really dislike using external programs :) Atleast for what the script will be for...

Share this post


Link to post
Share on other sites

Well I ended using this piece of code...the most basic way:

$InputBox = InputBox("Computer name", "Enter Computer name")
Run("control.exe sysdm.cpl,,1.ComputerNameChanges")
Sleep("1000")
Send("{TAB}")
Send("{TAB}")
Send("{SPACE}")
Send($InputBox,1)
Send("{TAB}")
Send("{TAB}")
Send("{UP}")
Send("{TAB}")
Send("domain.name",1)
Send("{TAB}")
Send("{SPACE}")
Sleep("1000")
Send("domain\user",1)
Send("{TAB}")
Send("password",1)
Send("{TAB}")
Send("{SPACE}")
Sleep("500")
WinwaitActive("Computer Name Changes")
Send("{SPACE}")
Send("{SPACE}")
Send("{TAB}")
Send("{SPACE}")

Not efficient, but it works..

Share this post


Link to post
Share on other sites

Well, after some time even script above started to fail.. so i'm refreshing this topic on how to join computer to domain without "netdom.exe"... Brainstorming welcome :P

Share this post


Link to post
Share on other sites

So I guess this topic is still a mystery to AutoIt .. :P

Share this post


Link to post
Share on other sites

After successfully renaming the computer you need to restart. After the restart join the domain.

Rename Method of the Win32_ComputerSystem Class

Return Value

Returns a 0 (zero) if successful. A nonzero return value indicates an error. If successful, a reboot is required.


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

After successfully renaming the computer you need to restart. After the restart join the domain.

Rename Method of the Win32_ComputerSystem Class

Return Value

Returns a 0 (zero) if successful. A nonzero return value indicates an error. If successful, a reboot is required.

I've taken that into account :P I _did_ reboot after rename. Same thing.

Share this post


Link to post
Share on other sites

I still have no idea why script below doesn't join computer to domain... It just exits with return code 0... Rename works fine.. Been strugling with this issue for whole week now ;(

Looking at MSDN JoinDomainOrWorkgroup Method of the Win32_ComputerSystem Class return code 0 mean successfully join the domain. Now looking at the join domain section of your script I will change msgbox fail @error for $ReturnValue, so you get the actually error code of why is failing to join the domain.

$ReturnValue = $oCmp.JoinDomainOrWorkGroup($sDomain, $sPassword, $sDomain & "\" & $sUser, $sOU, $JOIN_DOMAIN+$ACCT_CREATE)
        If $ReturnValue <> 0 Then
            MsgBox(0,"Fail","Join domain failed.  Error = " & @error & @CRLF & "Exiting script.")
            Exit
        Else
            MsgBox(0,"Success","Join domain successful.")
            MsgBox(0,"Reboot","Rebooting computer...")
            RunWait("shutdown -r -t 00")
        EndIf
    EndIf

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Can you let me know how you solved that? What code do you have if a machine already exists?

Share this post


Link to post
Share on other sites

#17 ·  Posted

May I ask why you don't want to use netdom.exe?

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I don't want to use netdom.exe because I want this function to be a part of installer without external exe's :) I never figured it out why it isn't working.. So I went for the Send() command, but that also eventualy turned out to be a crappy alternative. So this topic remains either to be solved or forgotten for good :party:

I found earlier today this topic, which only renames computer (that worked in my script too)...

http://www.autoitscript.com/forum/index.php?showtopic=95893

Edited by skysel

Share this post


Link to post
Share on other sites

This works for me.

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

    Const $DOMAIN = "'DOMAIN.LOCAL"                     ;change Domain
    Const $ADMINKENNWORT = "ADMINPASSWORD"   ;change PW
    Const $ADMIN = "Administrator"
    Const $COMPUTER = @ComputerName

    $objComputer = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & $COMPUTER & "\root\cimv2:Win32_ComputerSystem.Name='" & $COMPUTER & "'")
    $Ergebnis = $objComputer.JoinDomainOrWorkGroup($DOMAIN, $ADMINKENNWORT, $DOMAIN & "\" & $ADMIN, "", $JOIN_DOMAIN + $ACCT_CREATE)
    If ($Ergebnis <> 0) Then
        MsgBox(0, "Fehler beim beitritt in die Domain", $COMPUTER & " Fehler code: " & $Ergebnis )
    EndIf
2 people like this

Share this post


Link to post
Share on other sites

I never figured it out why it isn't working.. So I went for the Send() command, but that also eventualy turned out to be a crappy alternative.

I guess you didn't bother to try my suggestion in Post #14. Try the code below, it will give you the error code so you know why is not working. Just change the values of:

$strAdminPass = 'password'

$strAdminAccount = 'Administrator'

$strDomainName = "domain.com"

Global Const $JOIN_DOMAIN = 1
Global Const $ACCT_CREATE = 2

$strAdminPass = 'password'
$strAdminAccount = 'Administrator'

$strComputer = "."
$strDomainName = "domain.com"
 
$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colCompSystems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")

For $objCompSystem In $colCompSystems
 MsgBox(0, 'Joining Domain', "Computer Name: " & $objCompSystem.Name & @CRLF & @CRLF & "Domain - Before: " & $objCompSystem.Domain)
  
  $intJoinDomain = $objCompSystem.JoinDomainOrWorkgroup($strDomainName, $strAdminPass, $strDomainName & '\' & $strAdminAccount, '', $JOIN_DOMAIN + $ACCT_CREATE)
  If $intJoinDomain = 0 Then
    MsgBox(0, @ScriptName, "Joined computer to " & $strDomainName & " domain.")
  ElseIf $intJoinDomain = 1 Then
    MsgBox(0, @ScriptName, "Joined computer to " & $strDomainName & " domain." & @CRLF & "  Must reboot.")
  Else
    MsgBox(0, @ScriptName, "Unable to join computer to " & $strDomainName & " domain." & @CRLF & _GetExitCode($intJoinDomain)) 
  EndIf
Next

Func _GetExitCode($iExitCode)
    Local $sLine = ''
    $Pid = Run(@ComSpec & " /c net helpmsg " & $iExitCode, @SystemDir, @SW_HIDE, 2)
    While 1
        $sLine &= StdoutRead($Pid)
        If @error Then ExitLoop        
    Wend
     $strErrorMessage = "ErrorCode: " & $iExitCode & " " & StringStripWS(StringStripCR($sLine), 3)
    Return($strErrorMessage)
EndFunc

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

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