skysel Posted December 11, 2008 Share Posted December 11, 2008 (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 ;( expandcollapse popup;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 December 11, 2008 by skysel Link to comment Share on other sites More sharing options...
water Posted December 11, 2008 Share Posted December 11, 2008 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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
KaFu Posted December 11, 2008 Share Posted December 11, 2008 otherwise to see what the error is... hope this Error Handler works as I don't have the possibility to test expandcollapse popup;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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
skysel Posted December 11, 2008 Author Share Posted December 11, 2008 KaFu: First dialog is: "Join domain failed. Error = 0" Second is: "AutoItCom test: Test failed!" Link to comment Share on other sites More sharing options...
KaFu Posted December 11, 2008 Share Posted December 11, 2008 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).aspxWhat 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 . OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
skysel Posted December 15, 2008 Author Share Posted December 15, 2008 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).aspxWhat 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?? Link to comment Share on other sites More sharing options...
UniPer Posted December 15, 2008 Share Posted December 15, 2008 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, Link to comment Share on other sites More sharing options...
skysel Posted December 15, 2008 Author Share Posted December 15, 2008 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... Link to comment Share on other sites More sharing options...
skysel Posted December 15, 2008 Author Share Posted December 15, 2008 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.. Link to comment Share on other sites More sharing options...
skysel Posted March 2, 2009 Author Share Posted March 2, 2009 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 Link to comment Share on other sites More sharing options...
skysel Posted March 3, 2009 Author Share Posted March 3, 2009 So I guess this topic is still a mystery to AutoIt .. Link to comment Share on other sites More sharing options...
Danny35d Posted March 3, 2009 Share Posted March 3, 2009 After successfully renaming the computer you need to restart. After the restart join the domain.Rename Method of the Win32_ComputerSystem Class Return ValueReturns 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 Link to comment Share on other sites More sharing options...
skysel Posted March 3, 2009 Author Share Posted March 3, 2009 After successfully renaming the computer you need to restart. After the restart join the domain.Rename Method of the Win32_ComputerSystem Class Return ValueReturns a 0 (zero) if successful. A nonzero return value indicates an error. If successful, a reboot is required.I've taken that into account I _did_ reboot after rename. Same thing. Link to comment Share on other sites More sharing options...
Danny35d Posted March 3, 2009 Share Posted March 3, 2009 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 Link to comment Share on other sites More sharing options...
ty2d Posted March 24, 2009 Share Posted March 24, 2009 Take a look into this thread: http://www.autoitscript.com/forum/index.ph...;hl=join+domain - from there i got my code to join a computer to the domain - did not need the gui stuff.Only problem i didn't solve until now is what to do when a machine account already exists Link to comment Share on other sites More sharing options...
kor Posted April 3, 2009 Share Posted April 3, 2009 Can you let me know how you solved that? What code do you have if a machine already exists? Link to comment Share on other sites More sharing options...
TonyP Posted May 7, 2009 Share Posted May 7, 2009 May I ask why you don't want to use netdom.exe? Link to comment Share on other sites More sharing options...
skysel Posted June 1, 2009 Author Share Posted June 1, 2009 (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 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 June 1, 2009 by skysel Link to comment Share on other sites More sharing options...
Tec Posted June 2, 2009 Share Posted June 2, 2009 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 Duck and TekWeis 2 Link to comment Share on other sites More sharing options...
Danny35d Posted June 4, 2009 Share Posted June 4, 2009 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now