Jump to content

multiple variable sets into arrays to loop through a function


n3r0
 Share

Recommended Posts

Hello all, [insert newb apologies here].  I have built a gui to create Active Directory accounts en mass for auto-login systems.  As it currently exists, i have 24 sets of inputs (each having 3 radio button choices (device type) and a text input (account name).  i then have a function that creates the accounts as desired depending on user input.  My problem is that it's turned into a gargantuan function that is difficult to maintain efficiently.   as i am still new to this i ended up with 24 iterations of the code below just running in a line when the 'submit' button is pressed.  i feel like there has to be a more concise way to run this, and part of my brain says there is a way to load the variables into an array and loop a single function vs having 24 almost identical chunks of code running in a line, but i cannot figure out how to do it.  I am open to any ideas anyone might have.

 

  note, i'm using "AD 1.4.13.0" to make the AD changes.  

 

 thanks in advance for your help!

 

SOURCE: Applicable pieces of code (User input, case and acct creation function)

Note: $PC1, $VC1, and $SB1 are grouped radio buttons (3 for each account depending on device type) and $SystemName1 is the text input of the account name (total of 4 variables per account).  $locinput is from a dropdown that chooses the location of the system that determines the OU and $SB_OU (and SB_OU_parent) is a derivation of that, again for determining OU.  I redacted OU and domain names where necessary.  The code below is repeated for each of 24 possible sets of input.

 


;Device Info Input Device1
    GUIStartGroup()
    GUICtrlCreateLabel("Device Type", 10, 80, 160, "", 0x01)
    GUICtrlCreateLabel("Device Name", 180, 80, 120, "", 0x01)
    Global $PC1 = GUICtrlCreateRadio("PC", 10, 100)
    Global $VC1 = GUICtrlCreateRadio("VDI", 50, 100)
    Global $SB1 = GUICtrlCreateRadio("Status Board", 90, 100)
    Global $SystemName1 = GUICtrlCreateInput("", 180, 100, 120, "", 0x0008)

 

Case $idSubmit
                Variable_Create()
                If GUICtrlRead($LocInput)="" Then
                    MsgBox(0, "No Location Input", "No location was chosen, please choose a location.")
                    Else
                        Create_Accts()
                        Clear_Form_Input()
                    EndIf

 

Func Create_Accts()

;System1
        If GUICtrlRead($SystemName1)<>"" Then
            If GUICtrlRead($PC1)=1 Then
                _AD_Open()
                _AD_CreateUser("OU=" & GUICtrlRead($LocInput) & ",OU=_Auto Login Users,OU=*****,DC=*****,DC=org", GUICtrlRead($SystemName1), GUICtrlRead($SystemName1))
                _AD_SetPassword(GUICtrlRead($SystemName1), $PW)
                _AD_DisablePasswordExpire(GUICtrlRead($SystemName1))
                _AD_DisablePasswordChange(GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "GivenName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "DisplayName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "Description", "Autologin Acct: " & GUICtrlRead($SystemName1))
                _AD_AddUserToGroup($*****, GUICtrlRead($SystemName1))
                _AD_Close()
            ElseIf GUICtrlRead($VC1)=1 Then
                _AD_Open()
                _AD_CreateUser("OU=VDI,OU=" & GUICtrlRead($LocInput) & ",OU=_Auto Login Users,OU=*****,DC=*******,DC=org", GUICtrlRead($SystemName1), GUICtrlRead($SystemName1))
                _AD_SetPassword(GUICtrlRead($SystemName1), $PW)
                _AD_DisablePasswordExpire(GUICtrlRead($SystemName1))
                _AD_DisablePasswordChange(GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "GivenName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "DisplayName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "Description", "Autologin Acct: " & GUICtrlRead($SystemName1))
                _AD_AddUserToGroup("CN=VMware_View_*******,OU=View,OU=VMware,OU=_Groups,OU=*****,DC=**********,DC=org", GUICtrlRead($SystemName1))
            ElseIf GUICtrlRead($SB1)=1 Then
                _AD_Open()
                _AD_CreateUser("OU=" & $SBOU & $SB_OU_parent, GUICtrlRead($SystemName1), GUICtrlRead($SystemName1))
                _AD_SetPassword(GUICtrlRead($SystemName1), $PW)
                _AD_DisablePasswordExpire(GUICtrlRead($SystemName1))
                _AD_DisablePasswordChange(GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "GivenName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "DisplayName", GUICtrlRead($SystemName1))
                _AD_ModifyAttribute(GUICtrlRead($SystemName1), "Description", "Autologin Acct: Status Board - " & GUICtrlRead($SystemName1))
                _AD_AddUserToGroup("CN=**********,OU=RDSH,OU=VMware,OU=_Groups,OU=*******,DC=*********,DC=org", GUICtrlRead($SystemName1))
            EndIf
        Else
        EndIf

EndFunc

Link to comment
Share on other sites

Something like this? You can play with the script as the only function is to write the selected Radios and the entered systemname to the console.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $iCtrlStart = 75, $iCtrlHight = 40, $iOffset = 3, $iOffsetCol2 = 450
Global $hControls[24][4] ; 24 sets á 4 controls
$hGUI = GUICreate("Test", 780, 560)
GUICtrlCreateLabel("Device Type", 10, 40, 160, Default, "", 0x01)
GUICtrlCreateLabel("Device Name", 180, 40, 120, Default, "", 0x01)
For $i = 0 To 11
    GUICtrlCreateGroup("Group " & $i + 1, 5, $iCtrlStart + ($iCtrlHight * $i) - 10, 305, $iCtrlHight)
    GUIStartGroup()
    $hControls[$i][0] = GUICtrlCreateRadio("PC", 10, $iCtrlStart + ($iCtrlHight * $i) + $iOffset) ; $PC1
    $hControls[$i][1] = GUICtrlCreateRadio("VDI", 50, $iCtrlStart + ($iCtrlHight * $i) + $iOffset) ; $VC1
    $hControls[$i][2] = GUICtrlCreateRadio("Status Board", 90, $iCtrlStart + ($iCtrlHight * $i) + $iOffset) ; $SB1
    $hControls[$i][3] = GUICtrlCreateInput("", 180, $iCtrlStart + ($iCtrlHight * $i) + $iOffset, 120, Default, "", 0x0008) ; $SystemName1
Next
GUICtrlCreateLabel("Device Type", $iOffsetCol2 + 10, 40, 160, Default, "", 0x01)
GUICtrlCreateLabel("Device Name", $iOffsetCol2 + 180, 40, 120, Default, "", 0x01)
For $i = 12 To 23
    $j = $i - 12
    GUICtrlCreateGroup("Group " & $i + 1, $iOffsetCol2 + 5, $iCtrlStart + ($iCtrlHight * $j) - 10, 305, $iCtrlHight)
    GUIStartGroup()
    $hControls[$i][0] = GUICtrlCreateRadio("PC", 10 + $iOffsetCol2, $iCtrlStart + ($iCtrlHight * $j) + $iOffset) ; $PC1
    $hControls[$i][1] = GUICtrlCreateRadio("VDI", 50 + $iOffsetCol2, $iCtrlStart + ($iCtrlHight * $j) + $iOffset) ; $VC1
    $hControls[$i][2] = GUICtrlCreateRadio("Status Board", 90 + $iOffsetCol2, $iCtrlStart + ($iCtrlHight * $j) + $iOffset) ; $SB1
    $hControls[$i][3] = GUICtrlCreateInput("", 180 + $iOffsetCol2, $iCtrlStart + ($iCtrlHight * $j) + $iOffset, 120, Default, "", 0x0008) ; $SystemName1
Next
Global $hProcess = GUICtrlCreateButton("Process", 10, 10, 85, 25)
Global $hExit = GUICtrlCreateButton("Exit", $iOffsetCol2 + 10, 10, 85, 25)
; Display the GUI.
GUISetState(@SW_SHOW, $hGUI)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hExit
            ExitLoop
        Case $hProcess
            Create_Accts()
    EndSwitch
WEnd
; Delete the previous GUI and all controls.
GUIDelete($hGUI)

Func Create_Accts()
    Local $sSystemname, $bPC, $bVC, $bSB, $sLocInput
    ; _AD_Open()
    For $i = 0 To UBound($hControls) - 1
        $bPC = BitAND(GUICtrlRead($hControls[$i][0]), $GUI_CHECKED) = $GUI_CHECKED
        $bVC = BitAND(GUICtrlRead($hControls[$i][1]), $GUI_CHECKED) = $GUI_CHECKED
        $bSB = BitAND(GUICtrlRead($hControls[$i][2]), $GUI_CHECKED) = $GUI_CHECKED
        $sSystemname = GUICtrlRead($hControls[$i][3])
;       $sLocInput = GUICtrlRead($LocInput)
        If $sSystemname <> "" Then
            If $bPC Then
                ConsoleWrite("Group" & $i + 1 & ": " & $bPC & " selected for Systemname: " & $sSystemname & @CRLF)
                ; _AD_CreateUser("OU=" & $sLocInput & ",OU=_Auto Login Users,OU=*****,DC=*****,DC=org", $sSystemname, $sSystemName1)
                ; _AD_SetPassword($sSystemname, $PW)
                ; _AD_DisablePasswordExpire($sSystemname)
                ; _AD_DisablePasswordChange($sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "GivenName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "DisplayName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "Description", "Autologin Acct: " & $sSystemname)
                ; _AD_AddUserToGroup($ * * * * *, $sSystemname)
            ElseIf $bVC Then
                ConsoleWrite("Group" & $i + 1 & ": " & $bVC & " selected for Systemname: " & $sSystemname & @CRLF)
                ; _AD_CreateUser("OU=VDI,OU=" & $sLocInput & ",OU=_Auto Login Users,OU=*****,DC=*******,DC=org", $sSystemname, $sSystemname)
                ; _AD_SetPassword($sSystemname, $PW)
                ; _AD_DisablePasswordExpire($sSystemname)
                ; _AD_DisablePasswordChange($sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "GivenName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "DisplayName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "Description", "Autologin Acct: " & $sSystemname)
                ; _AD_AddUserToGroup("CN=VMware_View_*******,OU=View,OU=VMware,OU=_Groups,OU=*****,DC=**********,DC=org", $sSystemname)
            ElseIf $bSB Then
                ConsoleWrite("Group" & $i + 1 & ": " & $bSB & " selected for Systemname: " & $sSystemname & @CRLF)
                ; _AD_CreateUser("OU=" & $SBOU & $SB_OU_parent, $sSystemname, $sSystemname)
                ; _AD_SetPassword($sSystemname, $PW)
                ; _AD_DisablePasswordExpire($sSystemname)
                ; _AD_DisablePasswordChange($sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "GivenName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "DisplayName", $sSystemname)
                ; _AD_ModifyAttribute($sSystemname, "Description", "Autologin Acct: Status Board - " & $sSystemname)
                ; _AD_AddUserToGroup("CN=**********,OU=RDSH,OU=VMware,OU=_Groups,OU=*******,DC=*********,DC=org", $sSystemname)
            EndIf
        EndIf
    Next

EndFunc   ;==>Create_Accts

 

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

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...