Jump to content
Sign in to follow this  
badapple89

Simplify this array code?

Recommended Posts

badapple89

I have recently started using the AD.au3 file to do some work on AD. Works great, but I am having trouble simplifying my code.

So I want to look up a user and return there details (such as number, position, name) as variables to use later in my code.

The _AD_GetObjectProperties will return a 2d array with all the details I want, but I cant just use that array as row 1 = company, Row 2 = department Row 3 = displayname BECAUSE if the user doesn't have department filled in then Row 2 will = displayName

So depending on the users filled in details the array will always be different.

As you can see from my script below I create the array, search the array (returning row number), then use the row number to map to a variable.

If the search returns -1 (ie not found array) then the variable will just be blank.

However this seems really long, unnecicary and messy. What would be an easier, shorter way?

;#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
;#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
#include <AD.au3>
#include <Array.au3>

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

;Set veriables
Global $aProperties[1][2]
$vUsername = InputBox ("","Username")

;Get properties
$aProperties = _AD_GetObjectProperties($vUsername, "company,department,displayName,facsimileTelephoneNumber,givenname,info,mail,mobile,sn,telephoneNumber,Title,wWWHomePage")
_ArrayDisplay($aProperties, "Active Directory Functions - Example 2 - Properties for user '" & @UserName & "'")

;Search array for details and return the row in array
;Row number will cange depeending on feilds user has filled out
;Returns -1 if user does not a feild filled out
$arrCompany = _ArraySearch($aProperties, "company")
$arrDepartment = _ArraySearch($aProperties, "department")
$arrDisplayName = _ArraySearch($aProperties, "displayName")
$arrFacsimileTelephoneNumber = _ArraySearch($aProperties, "facsimileTelephoneNumber")
$arrGivenname = _ArraySearch($aProperties, "givenname")
$arrInfo = _ArraySearch($aProperties, "info")
$arrMail = _ArraySearch($aProperties, "mail")
$arrSn = _ArraySearch($aProperties, "sn")
$arrTelephoneNumber = _ArraySearch($aProperties, "telephoneNumber")
$arrTitle = _ArraySearch($aProperties, "Title")
$arrwWWHomePage = _ArraySearch($aProperties, "wWWHomePage")

MsgBox (0,"", $arrCompany & @LF & $arrdepartment & @LF & $arrdisplayName & @lf & $arrfacsimileTelephoneNumber & @LF & $arrgivenname& @LF & $arrinfo& @LF & $arrmail & @LF &$arrsn& @LF & $arrtelephoneNumber& @LF & $arrTitle& @LF & $arrwWWHomePage)

;Uses line row umber to convert data to string
If $arrCompany > 0 Then
    $Company = $aProperties[$arrCompany][1]
Else
    $Company = ""
Endif

If $arrDepartment > 0 Then
    $Department = $aProperties[$arrDepartment][1]
Else
    $Department = ""
Endif

If $arrDisplayName > 0 Then
    $DisplayName = $aProperties[$arrDisplayName][1]
Else
    $DisplayName=""
EndIf

If $arrFacsimileTelephoneNumber > 0 Then
    $FacsimileTelephoneNumber = $aProperties[$arrFacsimileTelephoneNumber][1]
Else
    $FacsimileTelephoneNumber = ""
EndIf

If $arrGivenname > 0 Then
    $Givenname = $aProperties[$arrGivenname][1]
Else
    $Givenname = ""
EndIf

If $arrInfo > 0 Then
    $Info = $aProperties[$arrInfo][1]
Else
    $Info = ""
EndIf

If $arrMail > 0 Then
    $Mail = $aProperties[$arrMail][1]
Else
    $Mail = ""
EndIf

If $arrSn > 0 Then
    $Sn = $aProperties[$arrSn][1]
Else
    $Sn = ""
EndIf

If $arrTelephoneNumber > 0 Then
    $TelephoneNumber = $aProperties[$arrTelephoneNumber][1]
Else
    $TelephoneNumber = ""
EndIf

If $arrTitle > 0 Then
    $Title = $aProperties[$arrTitle][1]
Else
    $Title = ""
EndIf

If $arrwWWHomePage > 0 Then
    $wWWHomePage = $aProperties[$arrwWWHomePage][1]
Else
    $wWWHomePage = ""
EndIf

MsgBox (0,"", $Company & @LF & $department & @LF & $displayName & @lf & $facsimileTelephoneNumber & @LF & $givenname& @LF & $info& @LF & $mail & @LF &$sn& @LF & $telephoneNumber& @LF & $Title& @LF & $wWWHomePage)

_AD_Close()

 

 

Share this post


Link to post
Share on other sites
spudw2k

If the results include the property name why don't you just evaluate the text?

If you are just looking to view the contents _ArrayDisplay will suffice.

If you are looking to use the results in a particular manner you can create a Switch or Select statement for each property/condition.

Edited by spudw2k

Share this post


Link to post
Share on other sites
badapple89

I dont just want to view the data (Already using _ArrayDisplay in the code just so can see what its like as I'm testing)

But I want to get each row:column two out of the array and to a separate strings. But the rows arnt constant depending on each user. IE Row 2 wont ALWAYS be department.

I had a look at switch as suggested but it appears I would use it in the same way I'm using if statements

Switch $arrDepartment
    Case > 0
        $Department = $aProperties[$arrDepartment][1]
    Case < 0
        $Department = ""
EndSwitch

Then repeat for each field that could exist.

 

Also select isn't doing what I want either.

My code IS working but its just messy and long

Edited by badapple89

Share this post


Link to post
Share on other sites
spudw2k

I guess what I am saying is do you really need to create separate variables/strings for each array element?

I can't guess how you intend to use those variables later but I was thinking having a switch statement to evaluate the property and do X with the correlating value would be an efficient way to handle the array. You know what I'm sayin?

;Array for Testing
Dim $arrProperties[4][2]=[["displayName","User Name"],["givenname","user"],["mail","email@comp.com"],["telephonenumber","800-555-1234"]]

For $x = 0 to UBound($arrProperties)-1
    Switch $arrProperties[$x][0]
        Case "displayName"
            ;Do Action with $arrProperties[$x][1]

        Case "givenname"
            ;Do Action with $arrProperties[$x][1]       

        Case "mail"
            ;Do Action with $arrProperties[$x][1]

        ;etc
    EndSwitch
Next

edit: darn edit broke my code box....fixed.

Edited by spudw2k

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×