Sign in to follow this  
Followers 0
fgilain

Help needed on Looping on an array and concate variables..

5 posts in this topic

Hello, i'm trying to develop a script using WMI that will display every Active directory user details by looping on an array that contains user's informations.

My problem is that i want to make easy to add or remove wanted attributs in result, so, i need to be able to give the list of wanted attributs in a simple string variable separated by ",".

Problem is that i'm unable to find the right syntax to get it work.

here is my code :

$ADShortDomainName = "MYDOMAIN"
$ADFullDnsDomainName = "MYDOMAIN.LOCAL"
$ListeAttributsRecherches = "company,name,department,title,telephoneNumber,facsimileTelephoneNumber,mobile,mail"
            $objWMI = ObjGet("winmgmts:\\" & $ADFullDnsDomainName & "\root\cimv2")
            $objAccount = $objWMI.Get("Win32_UserAccount.Name='" & $Login & "',Domain='" & $ADShortDomainName & "'")
            If IsObj($objAccount) Then
                ; Loop and display on every attributs defined in the "$ListeAttributsRecherches" variable
                $ArrayAttributs = StringSplit($ListeAttributsRecherches, ",")
                $UserDetails = ""
                For $attribut In $ArrayAttributs
                    $UserDetails = $UserDetails & $objAccount.$attribut & @CRLF
                Next

Thanks for your help !

Florent.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Problem is the line with :

$UserDetails = $UserDetails & $objAccount.$attribut & @CRLF

i get an error message : "Error in expression".

i need to generate a loop that will do something like :

$UserDetails = $UserDetails & $objAccount.company & @CRLF

$UserDetails = $UserDetails & $objAccount.name & @CRLF

$UserDetails = $UserDetails & $objAccount.department & @CRLF

etc... etc... using the attributs list in variable $ListeAttributsrecherches

Edited by fgilain

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Problem is the line with :

$UserDetails = $UserDetails & $objAccount.$attribut & @CRLF

i get an error message : "Error in expression".

i need to generate a loop that will do something like :

$UserDetails = $UserDetails & $objAccount.company & @CRLF

$UserDetails = $UserDetails & $objAccount.name & @CRLF

$UserDetails = $UserDetails & $objAccount.department & @CRLF

etc... etc... using the attributs list in variable $ListeAttributsrecherches

Maybe this would work

For $n = 1 to $ArrayAttributs[0]
    $UserDetails = $UserDetails & Execute("$objAccount." & $ArrayAttributs[$n]) & @CRLF
    Next
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Maybe this would work

For $n = 1 to $ArrayAttributs[0]
    $UserDetails = $UserDetails & Execute("$objAccount." & $ArrayAttributs[$n]) & @CRLF
    Next

Sorry, i just have tested, and it doesn't works too...

Florent

Share this post


Link to post
Share on other sites

Sorry, i just have tested, and it doesn't works too...

Florent

Then I think there is something else wrong because I believe that what I suggested should work, whereas what you had to begin with I would have predicted would not work.

You have not given any useful information about the result of testing what I suggested so

What error or result do you get?

What happens if you write the varaibles out one by one instead of using a loop? Eg as you suggested earlier-

$UserDetails = $UserDetails & $objAccount.company & @CRLF
$UserDetails = $UserDetails & $objAccount.name & @CRLF
$UserDetails = $UserDetails & $objAccount.department & @CRLF

If that works then does this work?

$UserDetails = $UserDetails &Execute("$objAccount.company") & @CRLF
 $UserDetails = $UserDetails & Execute("$objAccount.name") & @CRLF
 $UserDetails = $UserDetails & Execute("$objAccount.department") & @CRLF

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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  
Followers 0