Jump to content

HELP!! JoinDomain script stopped working


 Share

Recommended Posts

HELP!!! :)

We've been using the following JoinDomain script from Walt Howd for some time, and now all of a sudden, it has stopped working. When run, it is now displaying the following error message:

Domain Joined failed (1355)

I wasn't able to join the domain succssfully.

Up until recently, It had been working beautifully and succesfully joined about 1500 computers to the domain. My Domain Administrators say that nothing has changed on the domain side, so I'm left finding fault with the script. ;)

While troubleshooting I did come accross something that might be a problem. The following ObjGet call does not appear to pull any information:

;------------------------------------------------------------------------------
; Create our WMI object
;------------------------------------------------------------------------------
$computerObj  = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & _
     $computername & "\root\cimv2:Win32_ComputerSystem.Name='" & _
     $computername & "'")
Msgbox(0, "Objget Result", "$computerObj = " & $computerObj)

Could someone please give this a look-over and see if they can figure out what is going on? ;)

Thank you!

Here is the code:

;------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
;
; Script Function:
;  Join Domain - A GUI to join a Active Directory domain by specifying the OU
;
;  Requires:
;  Latest version of AutoIT with COM Support
;
;
; Author:        Walt Howd
;
; Version 1.0   03/04/2006  Initial internal release
; Version 1.1   05/05/2006  Small update. Added prompt for username and password.
;
; Copyright Walt Howd
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
;
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Includes and AutoIT Options
;------------------------------------------------------------------------------
#NoTrayIcon
#Include <GUIConstants.au3>
#Include <GuiListView.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Local $joinDomainResultText
;------------------------------------------------------------------------------
; Domain details
;
; Change these three lines to match your enviroment
;------------------------------------------------------------------------------
$adDefaultContext = "DC=MICROSOFT,DC=COM"
$adDomainController = "DC01.MICROSOFT.COM"
$adDomain  = "MICROSOFT"
;------------------------------------------------------------------------------
; Username and password
;
; If you wish to be prompted for these values instead of hardcoding them
; here you may leave these blank.
;------------------------------------------------------------------------------
$adUsername  = ""
$adPassword  = ""
;------------------------------------------------------------------------------
; Active Directory Constants
;------------------------------------------------------------------------------
Const $ldapConnectionString  = "LDAP://" & $adDomainController & "/" & $adDefaultContext
Const $ouIdentifier  = "OU="
Const $cnIdentifier  = "CN="
Const $ouSeparator  = ","
;------------------------------------------------------------------------------
; GUI Look and Feel Configuration
;------------------------------------------------------------------------------
$guiWidth  = 640
$guiHeight  = $guiWidth * 0.75
$guiOffset  = $guiHeight / 32
$guiBannerMessage = "Join Domain Tool"
$guiWindowTitle  = "Join Domain Tool"
$guiTooManyResultsTitle = "Too many results found - Please select one"
$guiJoinButtonTitle = "Join Domain"
$guiListOUSButtonTitle = "List all OUs"
$guiProgressMessage = "Please wait while querying Active Directory"
;------------------------------------------------------------------------------
; Prompt for the username if it is not hardcoded above
;------------------------------------------------------------------------------
If $adUsername = "" Then
$adUsername     = InputBox("Input username", "Please enter the username of the account you wish to use to " & _
     "join the domain. This should be just the username." & @LF & @LF & _
     "Example:" & @LF & "jdoe")
If @error Then
  MsgBox(0, "Error with username - " & @error, "There was an error proccessing your username." & @LF & @LF & _
    "The username I received was:" & @LF & _
    $adUsername & @LF & @LF & _
    "Please restart this application and try again. If this problem persists you can try" & _
    "hard coding this information into this program by editing the source file.")
  Exit(900)
EndIf
EndIf
;------------------------------------------------------------------------------
; Prompt for the password if it is not hardcoded above
;------------------------------------------------------------------------------
If $adPassword = "" Then
$adPassword     = InputBox("Input password","Please enter the password:",'','*')
If @error Then
  MsgBox(0, "Error with password - " & @error, "There was an error proccessing your password." & @LF & @LF & _
    "Please restart this application and try again. If this problem persists you can try" & _
    "hard coding this information into this program by editing the source file.")
  Exit(901)
EndIf
EndIf
;------------------------------------------------------------------------------
; Initiate our COM object to connect to Active Directory
;------------------------------------------------------------------------------
$comError   = ObjEvent("AutoIt.Error", "comError")
$ldapObj  = objGet("LDAP:")
$domain   = $ldapObj.OpenDsObject($ldapConnectionString, $adUsername, $adPassword, 1)
If @error Then
MsgBox(0, "Error connecting to Active Directory", "Error - " & @error & @LF & @LF & _
   "Could not connect to Active Directory." & @LF & @LF & _
   "Please check your network connectivity, that your LDAP connection string " & @LF & _
   "is set correctly and that the embedded credentials in this script are valid." & @LF & @LF & _
   "LDAP Connection String:" & @LF & _
   $ldapConnectionString & @LF & @LF & _
   "Username:" & @LF & _
   $adUsername)
GUIDelete()
Exit(1000)
EndIf
;------------------------------------------------------------------------------
; Initiate our ADO connection to Active Directory
;------------------------------------------------------------------------------
$connObj  = objCreate("ADODB.Connection")
$connObj.Provider = "ADsDSOObject"
$connObj.ConnectionTimeout = "1"
$connObj.Open("Active Directory Provider", $adDomain & "\" & $adUsername, $adPassword)
;------------------------------------------------------------------------------
; Find and store the computer name of the system running this program
;------------------------------------------------------------------------------
$computername = returnComputerName()
;------------------------------------------------------------------------------
; Create the GUI
;------------------------------------------------------------------------------
;$joinDomainGUI  = GUICreate($guiWindowTitle,$guiWidth,$guiHeight,-1,-1,BitOr($WS_MINIMIZEBOX,$WS_MAXIMIZEBOX,$WS_GROUP,$WS_CAPTION,$WS_POPUP,$WS_SYSMENU))
$joinDomainGUI  = GUICreate($guiWindowTitle,$guiWidth,$guiHeight,-1,-1,BitOr($WS_GROUP,$WS_CAPTION,$WS_POPUP))
;------------------------------------------------------------------------------
; Create the banner text
;------------------------------------------------------------------------------
$bannerText  = GuiCtrlCreateLabel($guiBannerMessage, $guiOffset,$guiOffset, $guiWidth*0.75)
$bannerTextFont  = GUICtrlSetFont($bannerText, 10, 600)
;------------------------------------------------------------------------------
; Create our Join Domain button
;------------------------------------------------------------------------------
$joinButton   = GUICtrlCreateButton ($guiJoinButtonTitle,$guiOffset,$guiOffset*3,($guiWidth/3),$guiOffset*2)
;------------------------------------------------------------------------------
; Create the TreeView - This is where all the OU's will be listed
;------------------------------------------------------------------------------
$defaultTreeView = GUICtrlCreateTreeView ($guiOffset,$guiOffset*10,($guiWidth-($guiOffset*2)),($guiHeight-($guiOffset*11)) )
;------------------------------------------------------------------------------
; Create the control to handle too many hits from an Object Search
; This will be hidden until a search with multiple hits is performed
;------------------------------------------------------------------------------
$tooManyObjectsList  = GuiCtrlCreateListView("Name|OU", $guiOffset,$guiOffset*10,($guiWidth-($guiOffset*2)),($guiHeight-($guiOffset*11)))
$hideTooManyObjectsList = GuiCtrlSetState($tooManyObjectsList, $GUI_HIDE);
;------------------------------------------------------------------------------
; Create the Progress Bar and Text controls
;------------------------------------------------------------------------------
$progressGroup  = GUICtrlCreateGroup("Progress", $guiOffset, $guiOffset*6, $guiWidth-($guiOffset*2), "54")
$progressBar  = GUICtrlCreateProgress($guiOffset*1.5,$guiOffset*7,($guiWidth-($guiOffset*3)),$guiOffset*1)
$progessBarStartAtZero  = GuiCtrlSetData ($progressBar, 0)
$progressText  = GuiCtrlCreateLabel($guiProgressMessage, $guiOffset*1.5,$guiOffset*8.5,($guiWidth-($guiOffset*3)),$guiOffset*1)
;------------------------------------------------------------------------------
; Create the Object search controls
;------------------------------------------------------------------------------
$objectSearchGroup = GUICtrlCreateGroup("Search", $guiWidth-($guiOffset*14), $guiOffset-5, "195", "80")
$objectSearchLabel = GuiCtrlCreateLabel("Enter username, computer or OU:", $guiWidth-($guiOffset*13), $guiOffset*2)
$objectSearchInput = GUICtrlCreateInput("", $guiWidth-($guiOffset*13), $guiOffset*3, "100")
$objectSearchButton = GUICtrlCreateButton("Search", $guiWidth-($guiOffset*5), $guiOffset*3)
;------------------------------------------------------------------------------
; Disable the buttons until our OU enumeration is done
;------------------------------------------------------------------------------
$disableJoinButton  = GUICtrlSetState($joinButton, $GUI_DISABLE);
$disableObjectSearchButton  = GUICtrlSetState($objectSearchButton, $GUI_DISABLE);
;------------------------------------------------------------------------------
; Make the GUI Visible
;------------------------------------------------------------------------------
$joinDomainGUIVisible = GUISetState(@SW_SHOW, $joinDomainGUI)
;------------------------------------------------------------------------------
; Query for all organizational units
;------------------------------------------------------------------------------
$allOUStringSepByLF = returnObjectDN("*", "organizationalUnit");
If $allOUStringSepByLF <> "0" Then
$allOUsArray   = StringSplit($allOUStringSepByLF, @LF, 1)
$deleteStringSplitCount = _ArrayDelete($allOUsArray, 0)
Else
MsgBox(0, "Could not query Active Directory", "Could not query Active Directory via ADO." & @LF & @LF & _
   "Please check your network connectivity, that your LDAP connection string " & @LF & _
   "is set correctly and that the embedded credentials in this script are valid." & @LF & @LF & _
   "LDAP Connection String:" & @LF & _
   $ldapConnectionString & @LF & @LF & _
   "Username:" & @LF & _
   $adUsername & @LF & @LF & _
   "Password:" & @LF & _
   $adPassword)
GUIDelete()
Exit(1001)
EndIf
;------------------------------------------------------------------------------
; Reverse the order of all the OUs so we can do an easy alpabetical sort
; on the names
;------------------------------------------------------------------------------
Dim $reversedAllOUsArray[1]
For $ou in $allOUsArray
$reversedOU  = returnReversedOU($ou)
$arrayAddStatus = _ArrayAdd($reversedAllOUsArray, $reversedOU)
Next
;------------------------------------------------------------------------------
; Sort the array alpabetically
;------------------------------------------------------------------------------
$sortRevArrayStatus = _ArraySort($reversedAllOUsArray)
;------------------------------------------------------------------------------
; Initialize our treeIDArray
; This will be used to look up the ID number of the GUI TreeView entries
; We need this to make nesting/child-parent relationships work.
;------------------------------------------------------------------------------
Dim $treeIDArray[UBound($reversedAllOUsArray)*2]
;------------------------------------------------------------------------------
; Return the OUs to their original format and create the TreeView Entries.
;------------------------------------------------------------------------------
$adCurrentObjectNumber = 0
For $reversedOU in $reversedAllOUsArray
If $reversedOU = "" then
  ContinueLoop
EndIf
;------------------------------------------------------------------------------
; Reverse the order back to the orginal format
;------------------------------------------------------------------------------
$ou = returnReversedOU($reversedou)
;------------------------------------------------------------------------------
; Calculate the additional values for this OU and check to see if its
; parentou has a GUI TreeID
;------------------------------------------------------------------------------
$fullOU = StringStripWS($ou, 3)
$friendlyOU   = returnFriendlyName($fullOU)
$parentFullOU   = returnParentOU($fullOU)
$parentTreeID   = _ArraySearch($treeIDArray, $parentFullOU)
;------------------------------------------------------------------------------
; Check if this OU has a parent. If it does make this TreeView item a child
; of that parent.
;------------------------------------------------------------------------------
If ( $parentTreeID <> "-1" ) Then
  $treeID   = GUICtrlCreateTreeViewItem ($friendlyOU, $parentTreeID)
Else
  $treeID   = GUICtrlCreateTreeViewItem ($friendlyOU, $defaultTreeView)
EndIf
;------------------------------------------------------------------------------
; The returned $treeID is sequential. We can use this to cheat and emulate
; a hashtable.
;------------------------------------------------------------------------------
$treeIDArray[$treeID] = $fullOU
;------------------------------------------------------------------------------
; Update the progress bar and increment the object count
;------------------------------------------------------------------------------
UpdateProgressBar(($adCurrentObjectNumber / UBound($allOUsArray)) * 100)
UpdateProgressText($guiProgressMessage & " - Currently querying " & $friendlyOU)
$adCurrentObjectNumber = $adCurrentObjectNumber + 1
Next
;------------------------------------------------------------------------------
; Query is complete, update Progress Text
;------------------------------------------------------------------------------
UpdateProgressText("Please select an OU to join from the list below.")
;------------------------------------------------------------------------------
; Check and see if this computername is already present in AD
;------------------------------------------------------------------------------
$computersCN = returnObjectDN($computername, "computer")
If $computersCN <> "0" Then
$computersOU = returnFullOUfromDN($computersCN)
UpdateProgressText("Found this computer (" & $computername & ") in the " & _
    returnFriendlyName($computersOU) & " OU - This has been selected " & _
    "for you below.")
GuiCtrlSetState(_ArraySearch($treeIDArray, $computersOU), $GUI_FOCUS)
EndIf
;------------------------------------------------------------------------------
; Enable the buttons
;------------------------------------------------------------------------------
$enableJoinButton  = GUICtrlSetState($joinButton, $GUI_ENABLE);
$enableObjectSearchButton  = GUICtrlSetState($objectSearchButton, $GUI_ENABLE);
;------------------------------------------------------------------------------
; GUI Message Loop
;------------------------------------------------------------------------------
While 1
$msg   = GUIGetMsg()
Select
  ;-----------------------------------------------------------------------------------
  ; User clicked "Close" button
  ;-----------------------------------------------------------------------------------
  Case $msg = -3 Or $msg = -1
   GUIDelete()
   ExitLoop
  ;-----------------------------------------------------------------------------------
  ; User clicked "Join Domain" button
  ;-----------------------------------------------------------------------------------
  Case $msg = $joinButton
   If GUICtrlRead($defaultTreeView) <> 0 Then
    $selectedOU = $treeIDArray[GUICtrlRead($defaultTreeView)]
    ;-----------------------------------------------------------------------------------
    ; Check if the OU selected matches OU in which this computer already exists (if any)
    ;
    ; If so prompt the user to see if they really want to join this different OU
    ;-----------------------------------------------------------------------------------
    $computername = returnComputerName()
    $computerDN = returnObjectDN($computername, "computer")
    $existingOU = returnFullOUfromDN($computerDN)
    If $existingOU <> "0" AND $existingOU <> $selectedOU Then
     $attemptJoin = MsgBox(4, "Computer exists in a different OU", "This computer already exists in a different OU." & @LF & @LF & _
     "A computer with this name (" & $computername & ") already exists in another OU." & @LF & @LF & _
     "The existing OU::" & @LF & _
     $existingOU & @LF & @LF & _
     "The OU you selected:" & @LF & _
     $selectedOU & @LF & @LF & _
     "Do you want to attempt to join the domain in this OU anway?")
     ;------------------------------------------------------------------------------
     ; If user clicked "No" skip out of the loop
     ;------------------------------------------------------------------------------
     If $attemptJoin = 7 Then
      ContinueLoop
     EndIf
    EndIf
    ;------------------------------------------------------------------------------
    ; Join the system to the domain
    ;------------------------------------------------------------------------------
    $joinDomainResult = joinDomain($selectedOU)
    ;------------------------------------------------------------------------------
    ; If domain join was successfull ask user if they want to restart system
    ;------------------------------------------------------------------------------
    If $joinDomainResult = 0 Then
     $restartQuery = MsgBox(4, "Restart system", "You joined the domain successfully." & @LF & @LF & _
         "Do you want to restart now?")
     if $restartQuery = 6 Then
      Shutdown(6)
     EndIf
     GuiDelete()
     Exit
    ;------------------------------------------------------------------------------
    ; If domain join failed notify user and try to give them a pertitent error
    ; message
    ;------------------------------------------------------------------------------
    Else
     MsgBox(0, "Domain Join Failed (" & $joinDomainResult & ")", "I wasn't able to join the domain successfully." & @LF & @LF & _
      $joinDomainResultText)
    EndIF
   Else
    ;------------------------------------------------------------------------------
    ; If the user forgot to select an OU from the TreeView
    ;------------------------------------------------------------------------------
    MsgBox(0, "Oopsies", "You must select an OU to join. Please select one from the list below.")
   EndIf
  ;-----------------------------------------------------------------------------------
  ; User searched for an Object
  ;-----------------------------------------------------------------------------------
  Case $msg = $objectSearchButton
   $objectSearchedFor  = StringStripWS(GUICtrlRead($objectSearchInput), 3)
   $objectsDN   = returnObjectDN($objectSearchedFor, "*")
   $objectsDNArray  = StringSplit($objectsDN, @LF)
   $deleteStringSplitCount = _ArrayDelete($objectsDNArray, 0)
   ;-----------------------------------------------------------------------------------
   ; If we found just one record, highlight it in the TreeView
   ;-----------------------------------------------------------------------------------
   If $objectsDN <> "0" AND UBound($objectsDNArray) = 1 Then
    $objectsOU = returnFullOUfromDN($objectsDN)
    GuiCtrlSetState(_ArraySearch($treeIDArray, $objectsOU), $GUI_FOCUS)
    UpdateProgressText("Found this object (" & $objectSearchedFor & ") in the " & _
           returnFriendlyName($objectsOU) & " OU - This has been selected " & _
           "for you below.")
   ;-----------------------------------------------------------------------------------
   ; If we found more then one record let the user choose which one they wanted
   ;-----------------------------------------------------------------------------------
   ElseIf $objectsDN <> "0" AND UBound($objectsDNArray) > 1 Then
    MsgBox(0, "Too many matching objects found", "Too many matching objects found for your search term (" & $objectSearchedFor & ")" & @LF & @LF & _
          "Please enter enough information to uniquely identify"  & @LF & _
          "a single object." & @LF & @LF & _
          "Objects that matches your search:" & @LF & _
           $objectsDN)
    ;-----------------------------------------------------------------------------------
    ; Delete any entries from the ListView control and then show the control
    ;-----------------------------------------------------------------------------------
    ;$deleteAllListViewItems = _GUICtrlListViewDeleteAllItems($tooManyObjectsList)
    ;$showTooManyObjectsList = GUICtrlSetState($tooManyObjectsList, $GUI_SHOW)
    ;-----------------------------------------------------------------------------------
    ; Disable the TreeView list of OUs and the JoinDomain button
    ;-----------------------------------------------------------------------------------
    ;$hideTreeView  = GUICtrlSetState($defaultTreeView, $GUI_HIDE)
    ;$disableJoinButton = GUICtrlSetState($joinButton, $GUI_DISABLE)
    ;-----------------------------------------------------------------------------------
    ; Update Progress Text
    ;-----------------------------------------------------------------------------------
    ;UpdateProgressText("Please select the object you were searching for from the list below:")
    ;For $objectName in $objectsDNArray
    ; $name  = returnFriendlyName($objectname);
    ; $ou = returnFullOUfromDN($objectname)
    ; GuiCtrlCreateListViewItem($name & "|" & $ou, $tooManyObjectsList)
    ;Next
   ;-----------------------------------------------------------------------------------
   ; If we didn't find any records
   ;-----------------------------------------------------------------------------------
   ElseIf $objectsDN = 0 Then
    MsgBox(0, "No matching objects found", "No objects were found for your search term (" & $objectSearchedFor & ")" & @LF & @LF & _
          "Please enter just the object name."  & @LF & @LF & _
          "Example:" & @LF & _
          "jdoe" & @LF & @LF & _
          "You may also use wildcards in your search." & @LF & @LF & _
          "Example:" & @LF & _
          "jdoe*")
   EndIf
EndSelect
WEnd
GUIDelete()
;------------------------------------------------------------------------------
; Shared Functions
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Joins the computer running this script to the domain $adDomain in the
; OU passed to the function
;------------------------------------------------------------------------------
Func joinDomain($joinDomainOU)
;------------------------------------------------------------------------------
; Make sure we got *something* passed as an OU
;------------------------------------------------------------------------------
If $joinDomainOU = "" OR ( StringLen($joinDomainOU) < StringLen($adDomain) ) Then
  MsgBox(0, "Invalid or no OU specified", "No OU was specified for the domain join." & @LF & @LF & _
    "It appears that this program did not correctly specify which OU you were attempting to join." & @LF & @LF & _
    "Please try again and if this error reappears check the configuration.")
EndIf
;------------------------------------------------------------------------------
; Create our WMI object
;------------------------------------------------------------------------------
$computerObj  = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & _
     $computername & "\root\cimv2:Win32_ComputerSystem.Name='" & _
     $computername & "'")
Msgbox(0, "Objget Result", "$computerObj = " & $computerObj)
;------------------------------------------------------------------------------
; Attempt domain join
;------------------------------------------------------------------------------
$joinDomainResultNumber = $computerObj.JoinDomainOrWorkGroup($adDomain, $adPassword, $adDomain & "\" & $adUsername, $joinDomainOU, 3)
;------------------------------------------------------------------------------
; Check our results
;------------------------------------------------------------------------------
Select
  Case $joinDomainResultNumber  = 0
   global $joinDomainResultText = "The domain join completed successfully."
  Case $joinDomainResultNumber  = 5
   global $joinDomainResultText = "The " & $adUsername & " account does not have permissions. Most likely this computer was originally joined to the domain with a Account operator account. The account you are using does not have permissions to overwrite the workstation object"
  Case $joinDomainResultNumber  = 86 Or $joinDomainResultNumber = 1326
   global $joinDomainResultText = "The username or password you specified is incorrect. Check the username and password as they appear below. If these values are incorrect the image will have to be updated with the current values"
  Case $joinDomainResultNumber  = 1909
   global $joinDomainResultText = "The " & $adUsername & " account is locked out. Most likely someone has tried the password incorrectly too many times. The system sometimes will lock the account out if too many simultaneous connection attempts are occuring."
  Case $joinDomainResultNumber  = 2224
   global $joinDomainResultText = "The computer account already exists on the domain and could not be overwritten. A workstation object with the same name exists in another OU. You will have to have the existing workstation object deleted."
  Case $joinDomainResultNumber  = 2453
   global $joinDomainResultText = "Could not find a domain controller. Please make sure all domain controllers are online and that your network access is functional."
  Case $joinDomainResultNumber  = 2102
   global $joinDomainResultText = "The workstation service is not started. Please start this service and try again."
  Case $joinDomainResultNumber  = 2691
   global $joinDomainResultText = "This machine is already joined to a domain. If you wish to join this system to another domain or this domain again you will first have to unjoin your existing domain."
EndSelect
return $joinDomainResultNumber
EndFunc
;------------------------------------------------------------------------------
; Searches AD via ADO for the DN of an object
;
;
; If the DN is found then it is returned.
;
; If no match is found then 0 is returned
;
;
; If more then one match is found then they are returned
; as a single string, separated by linebreaks.
;
; The second paramter dictates the object type to
; search for. It can either by user, computer, or
; organizationalunit. Alternatively it can be an objectclass
; your AD structure supports.
;
;------------------------------------------------------------------------------
Func returnObjectDN($objectName, $objectClass)
$objectsFoundCount   = 0
Dim $objectsFoundNames
;------------------------------------------------------------------------------
; Create the ADO object and execute the search
;------------------------------------------------------------------------------
$commandObj = objCreate("ADODB.Command")
$commandObj.ActiveConnection   = $connObj
$commandObj.CommandText   = "SELECT distinguishedName FROM '" & $ldapConnectionString & "' " & _
        "WHERE objectClass='" & $objectClass & "' AND name ='" & $objectName & "'"
$commandObj.Properties("Page Size")  = 10000
$commandObj.Properties("Cache Results") = True
$commandObj.Properties("SearchScope")  = 2
$objRecordSet   = objCreate("ADODB.Recordset")
$objRecordSet   = $commandObj.Execute
If($objRecordSet.RecordCount <> "") Then
  ;------------------------------------------------------------------------------
  ; Loop through the record set
  ;------------------------------------------------------------------------------
  $objRecordSet.MoveFirst
  While Not $objRecordSet.EOF
   If $objectsFoundCount > 0 Then
    $objectsFoundNames = $objectsFoundNames & @LF & $objRecordSet.Fields("distinguishedName").Value
   Else
    $objectsFoundNames = $objRecordSet.Fields("distinguishedName").Value
   EndIf
   $objRecordSet.MoveNext
   $objectsFoundCount= $objectsFoundCount+1
  WEnd
  If $objectsFoundCount > 0 Then
   return $objectsFoundNames
  Else
   return 0;
  EndIf
Else
  return 0;
EndIf
EndFunc
;------------------------------------------------------------------------------
;Reverse the order of all the OUs
;
; Example:
;
; The full OU of:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; DC=com,DC=domain,OU=Service Groups,OU=IT Shop
;
; We do this so we can do an easy alphabetical sort
;
; At this time ADO/ADSI doesn't support ORDER BY for
; organizational units
;------------------------------------------------------------------------------
Func returnReversedOU($ou)
$subOUArray = StringSplit($ou, $ouSeparator)
$reversedOU = ""
For $i = (UBound($subOUArray) - 1 ) To 1 Step -1
  If $i = (UBound($subOUArray) -  1 ) Then
   $reversedOU = $reversedOU & $subOUArray[$i]
  Else
   $reversedOU = $reversedOU & $ouSeparator & $subOUArray[$i]
  EndIf
Next
return $reversedOU
EndFunc
;------------------------------------------------------------------------------
; Replaces the Progress Text control with supplied text
;------------------------------------------------------------------------------
Func UpdateProgressText($text)
GUICtrlSetData($progressText, $text)
EndFunc
;------------------------------------------------------------------------------
; Updates the progress bar to the passed percentage
;------------------------------------------------------------------------------
Func UpdateProgressBar($percentage)
GuiCtrlSetData($progressBar, $percentage)
EndFunc
;------------------------------------------------------------------------------
; Will return the current computername of the system running this script
;------------------------------------------------------------------------------
Func returnComputerName()
$networkObj   = ObjCreate("WScript.Network")
$computerName   = $networkObj.ComputerName
return $computerName
EndFunc
;------------------------------------------------------------------------------
; Returns the friendly object name:
;
; Example:
;
; The full OU of:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; IT Shop
;------------------------------------------------------------------------------
Func returnFriendlyName($dn)
return StringMid($dn, StringInStr($dn, "=")+1, StringInStr($dn, $ouSeparator)-(StringInStr($dn, "=")+1))
EndFunc
;------------------------------------------------------------------------------
; Returns the full OU name from a DN record:
;
; Example:
;
; The DN:
; CN=Paula,OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;------------------------------------------------------------------------------
Func returnFullOUfromDN($dn)
If $dn <> "0" Then
  return StringRight($dn, StringLen($dn)-StringInStr($dn, $ouIdentifier)+1)
Else
  return 0
EndIf
EndFunc
;------------------------------------------------------------------------------
; Returns the parent OU name:
;
; Example:
;
; The full OU of:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; OU=Service Groups,DC=domain,DC=com
;------------------------------------------------------------------------------
Func returnParentOU($fullOU)
return StringRight($fullOU, ( StringLen($fullOU)-StringInStr($fullOU, $ouSeparator) ) )
EndFunc
;------------------------------------------------------------------------------
; Custom COM error handler
;
; Needs to be trapped with:
; $comError   = ObjEvent("AutoIt.Error", "comError")
;------------------------------------------------------------------------------
Func comError()
    If IsObj($comError) Then
        $hexNumber = Hex($comError.number, 8)
        SetError($hexNumber)
    EndIf
    Return 0
EndFunc
Link to comment
Share on other sites

To track down the problem you could use my Active Directory UDF, function _AD_JoinDomain. The script could so be reduced to the bare necessities and migth be easier to debug.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Domain Joined failed (1355)

I wasn't able to join the domain succssfully.

While troubleshooting I did come accross something that might be a problem. The following ObjGet call does not appear to pull any information:

Open command prompt and type NET USE HELPMSG 1355 then hit enter you get the following message "The specified domain either does not exist or could not be contacted."

If you can not find the specified domain the ObjGet won't be able to pull any information

;------------------------------------------------------------------------------
; Create our WMI object
;------------------------------------------------------------------------------
$computerObj  = ObjGet("winmgmts:{impersonationLevel=Impersonate}!" & _
     $computername & "rootcimv2:Win32_ComputerSystem.Name='" & _
     $computername & "'")
Msgbox(0, "Objget Result", "$computerObj = " & $computerObj)
Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

To track down the problem you could use my Active Directory UDF, function _AD_JoinDomain. The script could so be reduced to the bare necessities and migth be easier to debug.

Water,

Thanks for jumping in!

I've been playing around with one of your scripts to see if I can figure what is is going on.

#include <ad.au3>
$iAD_Debug = 2
Global $AdminUserName = "MicrosoftAdministrator"         ; Username with permissions to create computer accounts and set permisisons on the computer account in AD
Global $AdminUserPassword = "password"   ; Password
Global $DomainName = "DC=Microsoft,DC=COM"            ; Optional: Domain name e.g. DC=microsoft,DC=com
Global $DomainController = "DC01.Microsoft.com"   ; Optional: Name of Domain Controller e.g.DC-Server1.microsoft.com
Global $ConfigurationContext = "CN=Configuration,DC=microsoft,DC=com"   ; Optional: Configuration naming context e.g. CN=Configuration,DC=microsoft,DC=com
Global $OU = "OU=Computers,OU=Site,DC=Microsoft,DC=Com"                 ; Organizational unit to create the computer account in e.g. OU=Computer_Accounts,DC=microsoft,DC=com
Global $ComputerName = @ComputerName           ; Name of the computer account to create. SamAccountName without trailing "$" e.g. C0001
Global $Usergroup = "Administator"           ; Name of the user account or group to join the physical computer to the domain
_AD_Open($AdminUserName, $AdminUserPassword, $DomainName, $DomainController, $ConfigurationContext)
If @error Then Exit MsgBox(16, "ADAT", "_AD_Open: @error: " & @error & ", @extended: " & @extended)
_AD_CreateComputer($OU, $ComputerName, $Usergroup)
If @error Then Exit MsgBox(16, "ADAT", "_AD_CreateComputer: @error: " & @error & ", @extended: " & @extended)
MsgBox(64, "ADAT", "Computer successfully created!")
_AD_Close()

...and this is the error message I am getting:

_AD_Open: @error: 6, @extended: 0

Can you tell me what this mean?

Edited by HockeyFan
Link to comment
Share on other sites

Error code 6 means: Parameter $sAD_HostServerParam (parameter #4) and $sAD_ConfigurationParam (parameter #5) are required when $sAD_DNSDomainParam (parameter #3) is specified

Do you really work for microsoft? If not, you have to change the parameters in the script.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Error code 6 means: Parameter $sAD_HostServerParam (parameter #4) and $sAD_ConfigurationParam (parameter #5) are required when $sAD_DNSDomainParam (parameter #3) is specified

Do you really work for microsoft? If not, you have to change the parameters in the script.

water,

LOL! :)

No...I just removed my company's information and replaced it with generic stuff.

In reference to your response, do you mean:

; Syntax.........: _AD_Open([$sAD_UserIdParam = "", $sAD_PasswordParam = ""[, $sAD_DNSDomainParam = "", $sAD_HostServerParam = "", $sAD_ConfigurationParam = ""[, $fAD_Security = 0]]])
; Parameters ....: $sAD_UserIdParam - Optional: UserId credential for authentication. This has to be a valid domain user
;                 $sAD_PasswordParam - Optional: Password for authentication
;                 $sAD_DNSDomainParam - Optional: Active Directory domain name if you want to connect to an alternate domain e.g. DC=microsoft,DC=com
;                 $sAD_HostServerParam - Optional: Name of Domain Controller if you want to connect to a different domain e.g. DC-Server1.microsoft.com

..if so, isnt that what I have defined with the following?

$sAD_Computer = @ComputerName
$sAD_AdminUserName = "Microsoft\Administrator"
$sAD_AdminUserPassword = "password"
$sAD_DomainName = "DC=Microsoft,DC=COM"
$sAD_DomainController = "DC01.Microsoft.com"
$sAD_ConfigurationContext = "CN=Configuration,DC=Microsoft,DC=Com"
Link to comment
Share on other sites

Right, but error code 6 means that you didn't specify all necessary parameters. Might be a typo in the variable name ...

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Right, but error code 6 means that you didn't specify all necessary parameters. Might be a typo in the variable name ...

water,

Your brilliant! ;) Appears to be a typo on my end when I was changing everything back from the "generic" information. :)

Now, when the script runs, I get the following bebug information:

COM Error Encountered in ADAT-Testscript.au3

AD UDF version = 1.2.0

Scriptline = 2441

NumberHex = 80020009

Number = -2147352567

WinDescription = The security ID structure is invalid.

Descripton =

Source =

HelpFile =

HelpContext = 0

LastDllError = 0

I'm assuming the problem now is the "security ID structure is invalid" error. Can you possible enlighten me on what this mean?

Link to comment
Share on other sites

Function _AD_CreateComputer creates a computer account and then sets some permissions on this object.

Every user is allowed to add up to 10 computers to a domain. But not every domain user is allowed to set the permissions on the user account.

Make sure the user you use to connect to the AD has proper permissions to create and set permissions on an user account.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Function _AD_CreateComputer creates a computer account and then sets some permissions on this object.

Every user is allowed to add up to 10 computers to a domain. But not every domain user is allowed to set the permissions on the user account.

Make sure the user you use to connect to the AD has proper permissions to create and set permissions on an user account.

The account I am using to join the computer to the domain has the "Create computer objects" right and "authenticate user" right. What else does it need to prevent this error message?

Link to comment
Share on other sites

I'm not very firm with AD permissions.

I'm not sure but I think you need one or more of those rights listed here.

ADS_RIGHT_WRITE_DAC, ADS_RIGHT_GENERIC_WRITE, ADS_RIGHT_DS_CONTROL_ACCESS?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

water,

Sorry to bother you again, but I'd like to ask for your help once more. :) I can create another thread in the General forum, if you'd like, but I figured I'd give it a shot since it's something you've worked with previoulsy.

I've given up on the error message from the previous posts becasue it seems to not interfere with the success of the process. Now I'm just working to get my script to work properly. The problem I'm running into is how to test for the lack of a selected item from the TreeView of displayed OU's.

Would you might taking a look at my script and seeing where I'm off base? ;)

#Include <GUIConstants.au3>
#Include <GuiListView.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <TabConstants.au3>
#include <EditConstants.au3>
#include <AD.au3>
#include <StaticConstants.au3>
Opt("GUIOnEventMode", 1)  ;0=disabled, 1=OnEvent mode enabled
HotKeySet("+!t", "Terminate") ;Shift-Alt-t
$sAD_Computer = @ComputerName
$sAD_UserParm = ""
$sAD_User = ""
$sAD_PasswordParm = ""
$sAD_AdminUserName = ""
$sAD_AdminUserPassword = ""
$sAD_DomainName = ""
$sAD_DomainController = ""
$sAD_ConfigurationContext = ""
$sAD_SelectedOU = ""
Global $bIsConnected = False
Global $sTitle = "ADAT"
Local $Msg, $hSelection
Local $sOU = "" ; FQDN of the OU where to start
Local $sTitle = "Active Direcory OU Treeview"
Local $defaultTreeView = ""
Local $aTreeView = ""
$guiWidth  = 640
$guiHeight  = 480
$guiOffset  = 15
$guiBannerLength = 600
$guiBannerMessage = "Join Domain Utility"
$guiWindowTitle  = ""
$guiTooManyResultsTitle = "Too many results found - Please select one"
$guiJoinButtonTitle = "Join Domain"
$guiListOUSButtonTitle = "List all OUs"
$joinDomainGUI  = GUICreate($guiWindowTitle,$guiWidth,$guiHeight,-1,-1,BitOr($WS_GROUP,$WS_CAPTION,$WS_POPUP))
$bannerText = GuiCtrlCreateLabel($guiBannerMessage, 15, 10, $guiBannerLength, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE), $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0xff0000)
GUICtrlSetFont(-1, 12, 600, 0, "Tahoma")
$joinButton = GUICtrlCreateButton ("Join Domain",15,52,(($guiBannerLength/2)-7),30)
$bSelectOU = GUICtrlCreateButton("Select OU", (($guiWidth/2)+5), 52,(($guiBannerLength/2)-8), 30)
$defaultTreeView = GUICtrlCreateTreeView ($guiOffset,$guiOffset*10,($guiWidth-($guiOffset*2)),($guiHeight-($guiOffset*11)), BitOR($GUI_SS_DEFAULT_TREEVIEW,$WS_BORDER), $WS_EX_STATICEDGE)
$tooManyObjectsList  = GuiCtrlCreateListView("Name|OU", $guiOffset,$guiOffset*10,($guiWidth-($guiOffset*2)),($guiHeight-($guiOffset*11)))
$hideTooManyObjectsList = GuiCtrlSetState($tooManyObjectsList, $GUI_HIDE);
$OUSelectedGroup  = GUICtrlCreateGroup("OU Where computer will be joined to:", $guiOffset, $guiOffset*6, $guiWidth-($guiOffset*2), "54")
$OUSelectedInput = GUICtrlCreateInput("", 22, 110, 595, 25, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$disableJoinButton = GUICtrlSetState($joinButton, $GUI_DISABLE);
GUICtrlSetState($bSelectOU, $GUI_DISABLE)
$joinDomainGUIVisible = GUISetState(@SW_SHOW, $joinDomainGUI)
GUICtrlSetOnEvent($joinButton, "_JoinToDomain")
GUICtrlSetOnEvent($bSelectOU, "SelectedOU")
If $bIsConnected Then _AD_Close()
$bIsConnected = False
_AD_Open($sAD_AdminUserName, $sAD_AdminUserPassword, $sAD_DomainName, $sAD_DomainController, $sAD_ConfigurationContext)
If @error Then
Local $sError = @error, $sExtended = @extended
Local $sAD_Hive = "HKLM", $sErrorMsg
If @OSArch = "IA64" Or @OSArch = "X64" Then $sAD_Hive = "HKLM64"
Local $sAD_OSVersion = RegRead($sAD_Hive & "\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion")
$sAD_OSVersion = StringSplit($sAD_OSVersion, ".")
If Int($sAD_OSVersion[1]) >= 6 Then ; Delivers detailed error information for Windows Vista and later if debugging is activated
  Local $aAD_Errors = _AD_GetLastADSIError()
  If $aAD_Errors[4] <> 0 Then $sErrorMsg = $aAD_Errors[5]
  EndIf
  MsgBox(16, $sTitle, "Error accessing the Active Directory. @Error: " & $sError & ", @extended: " & $sExtended & @CRLF & $sErrorMsg)
EndIf
$bIsConnected = True
Local $aTreeView = _AD_GetOUTreeView($sOU, $defaultTreeView, True)
If @error <> 0 Then
MsgBox(16, "Active Direcory OU Treeview", "Error creating list of OUs starting with '" & $sOU & "'." & @CRLF & _
   "Error returned by function _AD_GetALLOUs: @error = " & @error & ", @extended =  " & @extended)
EndIf
While 1
Sleep(50)
WEnd
If $bIsConnected Then _AD_Close()
Exit
Func _JoinToDomain()
;MsgBox(0,"selected OU:", $sAD_SelectedOU)
$iResult = _AD_CreateComputer($sAD_SelectedOU, $sAD_Computer , $sAD_User)
If $iResult <> 1 Then
   Switch @error
    Case 1
     MsgBox(16, "Creating Computer Object", "Specified OU does not exist!")
    Case 2
     MsgBox(16, "Creating Computer Object", "The computer is already defined in the specified OU!")
    Case 3
     MsgBox(16, "Creating Computer Object", "Specified user/group does not exist!")
    Case Else
     ;MsgBox(16, "Creating Computer Object", "Error creating computer. @error: " & @error & ", @extended: " & @extended)
   EndSwitch
  Else
   $iResult = _AD_ModifyAttribute($sAD_Computer & "$", "description")
   If $iResult <> 1 Then
    Switch @error
     Case 1
      MsgBox(16, "Modifying Computer Attribute", "Specified computer does not exist!")
     Case Else
      MsgBox(16, "AD TestModifying Computer Attribute", "Error setting description. @error: " & @error & ", @extended: " & @extended)
    EndSwitch
   EndIf
  EndIf
$iResult = _AD_JoinDomain($sAD_Computer, $sAD_UserParm, $sAD_PasswordParm)
  If $iResult <> 1 Then
   Switch @error
    Case 1
     MsgBox(16, "Error Joining Domain", "Computer account does not exist in the domain!")
    Case 3
     MsgBox(16, "Error Joining Domain", "WMI object could not be created! @error: " & @error & ", @extended: " & @extended)
    Case 4
     MsgBox(16, "Error Joining Domain", "The computer is already a member of the domain!")
    Case Else
     MsgBox(16, "Error Joining Domain", "Joining the domain was not successful. @error: " & @error & ", @extended: " & @extended)
   EndSwitch
  EndIf
_AD_Close()
MsgBox(16, "Join Domain Utility (v2)", " Computer: " & $sAD_Computer & @CRLF & " has been successfully joind to the ADA Domain!." & @CRLF & " Computer was joined to the following OU Location: " & @CRLF & " " & $sAD_SelectedOU)
Exit
EndFunc
Func SelectedOU()
$hSelection = _GUICtrlTreeView_GetSelection($defaultTreeView)
For $i = 1 To $aTreeView[0][0]
  If $hSelection = $aTreeView[$i][2] Then ExitLoop
Next
MsgBox(0,"", $aTreeView[$i][1])
If $hSelection = 0 Then
  MsgBox(0, "ERROR", "You must select an OU to join. Please select one from the list below.")
Else
  GUICtrlSetData($OUSelectedInput, $aTreeView[$i][1])
  GUICtrlSetState($joinButton, $GUI_ENABLE)
EndIf
EndFunc
Func _AD_GetOUTreeView($sAD_OU, $hAD_TreeView, $bAD_IsADOpen = False)
If $bAD_IsADOpen = False Then
  _AD_Open()
  If @error Then Return SetError(@error, @extended, 0)
EndIf
Local $sSeparator = "\", $aAD_Temp, $sAD_Line, $iAD_Level
Local $aAD_OUs = _AD_GetAllOUs($sAD_OU, $sSeparator)
If @error <> 0 Then Return SetError(@error, @extended, 0)
Local $aAD_TreeView[$aAD_OUs[0][0] + 1][3] = [[$aAD_OUs[0][0], 3]]
For $i = 1 To $aAD_OUs[0][0]
  $aAD_Temp = StringSplit($aAD_OUs[$i][0], $sSeparator)
  $aAD_TreeView[$i][0] = StringFormat("%" & $aAD_Temp[0] - 1 & "s", "") & "#" & $aAD_Temp[$aAD_Temp[0]]
  $aAD_TreeView[$i][1] = $aAD_OUs[$i][1]
Next
If $bAD_IsADOpen = False Then _AD_Close()
_GUICtrlTreeView_BeginUpdate($hAD_TreeView)
Local $ahAD_Node[50]
For $iAD_Index = 1 To $aAD_TreeView[0][0]
  $sAD_Line = StringSplit(StringStripCR($aAD_TreeView[$iAD_Index][0]), @TAB)
  $iAD_Level = StringInStr($sAD_Line[1], "#")
  If $iAD_Level = 0 Then ExitLoop
  If $iAD_Level = 1 Then
   $ahAD_Node[$iAD_Level] = _GUICtrlTreeView_Add($hAD_TreeView, 0, StringMid($sAD_Line[1], $iAD_Level + 1))
   $aAD_TreeView[$iAD_Index][2] = $ahAD_Node[$iAD_Level]
  Else
   $ahAD_Node[$iAD_Level] = _GUICtrlTreeView_AddChild($hAD_TreeView, $ahAD_Node[$iAD_Level - 1], StringMid($sAD_Line[1], $iAD_Level + 1))
   $aAD_TreeView[$iAD_Index][2] = $ahAD_Node[$iAD_Level]
  EndIf
Next
_GUICtrlTreeView_EndUpdate($hAD_TreeView)
GUICtrlSetState($bSelectOU, $GUI_ENABLE)
Return $aAD_TreeView
EndFunc
Func Terminate()
If $bIsConnected Then _AD_Close()
Exit 0
EndFunc

Thanks again for all of your help!

Link to comment
Share on other sites

I assume you've taken the function _AD_GetOUTreeView from the Active Directory Example Scripts thread.

Use the part of the code preceding the function (the While/Wend part). It sets the variable with the selected OU. You can check this variable for empty.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

I assume you've taken the function _AD_GetOUTreeView from the Active Directory Example Scripts thread.

Use the part of the code preceding the function (the While/Wend part). It sets the variable with the selected OU. You can check this variable for empty.

Water,

I'm a bit confused as to why your example script displays with the top OU highlighted, but mine does not. ;)

Also, with nothing selected on my script in the TreeView, when I test for $sSelection, it comes back with the text of the top OU. When I test for $hSelection, it comes back with 0x00000000.

Func SelectedOU()
$hSelection = _GUICtrlTreeView_GetSelection($defaultTreeView)
$sSelection = _GUICtrlTreeView_GetText($defaultTreeView, $hSelection)
MsgBox(0,"", $hSelection)
Msgbox(0,"", $sSelection)
For $i = 1 To $aTreeView[0][0]
  If $hSelection = $aTreeView[$i][2] Then ExitLoop
Next
MsgBox(0,"", $aTreeView[$i][1])
If $hSelection = "" Then
  MsgBox(0, "ERROR", "You must select an OU to join. Please select one from the list below.")
Else
  GUICtrlSetData($OUSelectedInput, $aTreeView[$i][1])
  GUICtrlSetState($joinButton, $GUI_ENABLE)
EndIf
EndFunc

I guess I'm a bit lost as to how to test the variable for empty. :)

Any suggestions?

Link to comment
Share on other sites

Can't test at the moment - my Ubuntu PC at home doesn't understand AutoIt and Active Directory :)

Will test as soon as I'm in my office again.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Please change function SelectedOU like this:

Func SelectedOU()
    $hSelection = _GUICtrlTreeView_GetSelection($defaultTreeView)
    If $hSelection = 0 Then Return MsgBox(16, "Select OU", "No OU has been selected! Try again!")
    For $i = 1 To $aTreeView[0][0]
        If $hSelection = $aTreeView[$i][2] Then ExitLoop
    Next
    GUICtrlSetData($OUSelectedInput, $aTreeView[$i][1])
    GUICtrlSetState($joinButton, $GUI_ENABLE)
EndFunc   ;==>SelectedOU

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

BTW:

Your script is missing an Exit button. I always had to kill the script to exit.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Please change function SelectedOU like this:

Func SelectedOU() $hSelection = _GUICtrlTreeView_GetSelection($defaultTreeView) If $hSelection = 0 Then Return MsgBox(16, "Select OU", "No OU has been selected! Try again!") For $i = 1 To $aTreeView[0][0] If $hSelection = $aTreeView[$i][2] Then ExitLoop Next GUICtrlSetData($OUSelectedInput, $aTreeView[$i][1]) GUICtrlSetState($joinButton, $GUI_ENABLE) EndFunc ;==>SelectedOU
water,

THANK YOU SO MUCH for your help...it works perfectly. :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...