LisHawj

_AD_CreateComputer unable to create computer object

4 posts in this topic

Good morning,

I wrote the following script to assist non-IT team members to quickly add new computer account/objects into Active Directory, but the script is unable to create the computer object.  The script kicks up an error that OU does not exist but the OU does exist.  Using the default example from the help file for _AD_Createcomputer and pasting the same OU information into the field works.  However, my script does not work and right about now a second set or third set of eyes is welcomed.  I have included the entire script below for review that I am using to test against our TEST domain.  Your time and advise is much appreciated.  Thank you.

Note:  I borrowed the combo box code/format from a member (AutoBert) on this Forum.

; Script Start - Add your code below here
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <Ad.au3>

$hGUI_ADCPC = GUICreate("Active Directory create new computer", 671, 109, 328, 182)
GUICtrlCreateLabel("Select Active Directory OU:", 264, 10, 135, 17)
GUICtrlCreateLabel("Destination OU:", 8, 42, 79, 18)
GUICtrlCreateLabel("Computer Name:", 8, 10, 79, 17)
$ComputerName = GUICtrlCreateInput("", 97, 8, 139, 21)
$Destination_OU = GUICtrlCreateInput("", 97, 40, 563, 21)
$BOK = GUICtrlCreateButton("Create Computer", 96, 66, 121, 33)
$BCancel = GUICtrlCreateButton("Cancel", 244, 66, 73, 33)
$Combo1 = GUICtrlCreateCombo("", 400, 8, 257, 100)
GUISetState(@SW_SHOW)

GUICtrlSetState($BOK, $GUI_DISABLE)

Global $List1, $Destination_OU, $OUs, $sComputer, $sOU_String
Global $sSelect = "Please select an OU"
Global $sOU_ini = "F:\OU.INI"

If Not FileExists($sOU_ini) Then
    $sData = "Madison=OU=Madison,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Manitowoc=OU=Manitowoc,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "GreenBay=OU=GreenBay,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Milwaukee=OU=Milwaukee,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Troy=OU=Troy,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Brookfield=OU=Brookfield,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "HolyHill=OU=HolyHill,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Oshkosh=OU=Oshkosh,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Appleton=OU=Appleton,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    $sData &= "Cudahy=OU=Cudahay,OU=MidWest,OU=LGD,DC=TEST,DC=com" & @LF
    IniWriteSection($sOU_ini, "OU", $sData)
EndIf

Read_Ou()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BOK
            ; Read and verify values are captured correctly (computer name and OU)
            $sComputer = GUICtrlRead($ComputerName) ; This value is passed to the _AD_CreateComputer function.
            $sOU = GUICtrlRead($Destination_OU) ; This value is passed to the _AD_CreateComputer function.
            $sCN = Guictrlread($ComputerName)
            $sOU2 = Guictrlread($Destination_OU)
            Consolewrite($sCN & @CRLF)
            Consolewrite($sOU2 & @CRLF)
            Cred_auth()
        Case $BCancel
            Stop()
        Case $Combo1
            Display_OU()
    EndSwitch
WEnd

Func Read_Ou()
    ; Reads the OU ini file and set data into combo1.
    $List1 = IniReadSection($sOU_ini, "OU")
    ConsoleWrite($List1 & @CRLF)
    If IsArray($List1) Then
        For $i = 1 To $List1[0][0]
            GUICtrlSetData($Combo1, $List1[$i][0])
        Next
    EndIf
    _GUICtrlComboBox_InsertString($Combo1, $sSelect, 0)
    _GUICtrlComboBox_SetCurSel($Combo1, 0)
EndFunc   ;==>Read_Ou

Func Display_OU()
    ; Checks to see if any OU is selected, if no OU is selected then keep the Create Computer button disabled.
    $iReturn = GUICtrlRead($Combo1)
    If $iReturn = $sSelect Then
        GUICtrlSetState($BOK, $GUI_DISABLE)
        GUICtrlSetData($Destination_OU, "")
    Else
    ; If an OU is selected, then enable the Create Computer button and set OU data into combo1 and consolewrite to show proper data value is captured.
        GUICtrlSetState($BOK, $GUI_ENABLE)
        $Ou_List = GUICtrlRead($Combo1)
        ConsoleWrite("selected: " & $Ou_List & @CRLF)
        $OU_string = IniRead($sOU_ini, "OU", $Ou_List, "")
        GUICtrlSetData($Destination_OU, $OU_string)
        ConsoleWrite("OU: " & $OU_string & @CRLF)
    EndIf
EndFunc   ;==>Display_OU

Func Cred_Auth()
    Global $hGUI_Auth = GUICreate("Login to Active Directory", 305, 146, -1, -1)
    Global $sNameInput = GUICtrlCreateInput("", 103, 48, 153, 21)
    Global $sPassInput = GUICtrlCreateInput("", 103, 80, 153, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
    Global $BOK = GUICtrlCreateButton("Ok", 183, 104, 75, 25)
    Global $Label1 = GUICtrlCreateLabel("Username", 47, 50, 52, 17)
    Global $Label2 = GUICtrlCreateLabel("Password", 47, 82, 50, 17)
    Global $Label3 = GUICtrlCreateLabel("Testing credential:", 8, 16, 213, 20)
    GUISetState(@SW_SHOW)

    ; AD_UDF variables
    Global $sTEST = "TEST\"
    Global $sHostSrv = "F:\hostsrv.ini"
    Global $sDNSdomainParam = "DC=TEST,DC=com"
    Global $sHostServerParam = ""
    Global $sConfigurationParam = "CN=Configuration,DC=TEST,DC=com"
    Global $sUsername, $sPassword

    If Not FileExists($sHostSrv) Then
        $sIniCreate = FileOpen($sHostSrv, 2)
        FileClose($sIniCreate)
        IniWrite($sHostSrv, "DNS", "1", "Applewood")
    EndIf

    $sDNS = IniRead($sHostSrv, "DNS", "1", "")
    Select
        Case $sDNS = "Applewood"
            Assign("sHostServerParam", "SDCAPPLEWOOD.TEST.COM")
        Case $sDNS = "Cedarwood"
            Assign("sHostServerParam", "SDCCEDARWOOD.TEST.COM")
        Case $sDNS = "Pinewood"
            Assign("sHostServerParam", "SDCPINEWOOD.TEST.COM")
    EndSelect

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($hGUI_Auth)
                Return
            Case $BOK
                $sUsername = GUICtrlRead($sNameInput) ; This value is passed to the _AD_CreateComputer function.
                $sPassword = GUICtrlRead($sPassInput)

                ConsoleWrite($sOU & @CRLF) ; Check data value by returning value in console.
                ConsoleWrite($sComputer & @CRLF) ; Check data value by returning value in console.
                ConsoleWrite($sUsername & @CRLF) ; Check data value by returning value in console.

                ;Opens AD connection to query object
                _AD_Open($sTEST & $sUsername, $sPassword, $sDNSdomainParam, $sHostServerParam, $sConfigurationParam)
                If @error Then MsgBox(16, "Active Directory search result:", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

                ;Creates new PC object into Active Directoy.  *** Action reports the OU does not exist when though it does.***
                _AD_CreateComputer($sOU, $sComputer, $sUsername)
                If @error Then MsgBox(16, "Active Directory search result:", "Function _AD_CreateComputer encountered a problem. @error = " & @error & ", @extended = " & @extended)
                GUIDelete($hGUI_Auth)
                _AD_Close()
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>Cred_Auth

Func Stop()
    Exit
EndFunc   ;==>Stop

 

 

Share this post


Link to post
Share on other sites



Quite a long script to debug ;)
Try something more simple, like:

#include <AD.au3>
Global $sTEST = "...", $sUsername = "...", $sPassword = "...", $sDNSdomainParam = "...", $sHostServerParam = "...", $sConfigurationParam = "..."
Global $sOU = "...", $sComputer = "...")
_AD_Open($sTEST & $sUsername, $sPassword, $sDNSdomainParam, $sHostServerParam, $sConfigurationParam)
If @error Then MsgBox(16, "Error", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
_AD_CreateComputer($sOU, $sComputer, $sUsername)
If @error Then MsgBox(16, "Error", "Function _AD_CreateComputer encountered a problem. @error = " & @error & ", @extended = " & @extended)
_AD_Close()

 

1 person likes this

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

Issue resolved!  The problem lies in the value for $sDNSdomainParam.    "Quite a long script to debug ;)" - this comment made me go line by line and character by character....lol but I found the problem.  Thank you!

Global $sDNSdomainParam = "DC=TEST,DC=com" ; Had this for the dnsdomain parameter.  TEST is no longer in service and explains why the script is unable to locate the OUs.
  Global $sDNSdomainParam = "DC=ElronSrv,DC=com" ; Should had been this instead.

 

Share this post


Link to post
Share on other sites

Glad to be of service :)


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

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