Link to home
Start Free TrialLog in
Avatar of Fr. Vandecan
Fr. VandecanFlag for Belgium

asked on

Function returning an array - not working why ?

function PopulateArrAD
{
      $ArrLocal = [System.Collections.ArrayList]@()

      $ArrLocal.Add(("StreetAddress",(GetItemFromAdUser $strUser "StreetAddress")))
      $ArrLocal.Add(("PostalCode",(GetItemFromAdUser $strUser "PostalCode")))
      $ArrLocal.Add(("City",(GetItemFromAdUser $strUser "City")))
      $ArrLocal.Add(("TelephoneNumber" ,(GetItemFromAdUser $strUser "TelephoneNumber")))
      $ArrLocal.Add(("Fax",(GetItemFromAdUser $strUser "Fax")))
      $ArrLocal.Add(("State", (GetItemFromAdUser $strUser "State")))
      $ArrLocal.Add(("ADOU", (GetOuUser $strUser)))
      $ArrLocal.Add(("SiteCode", (GetCustomAttribute $strUser "ExtensionAttribute1")))
      $ArrLocal.Add(("City", (GetItemFromAdUser $strUser "City")))
      $ArrLocal.Add(("Title", (GetItemFromAdUser $strUser "Title")))
      $ArrLocal.Add(("Department", (GetItemFromAdUser $strUser "Department")))
      $ArrLocal.Add(("Manager", (GetItemFromAdUser $strUser "Manager")))
      $ArrLocal.Add(("Contrat_Nb",(GetCustomAttribute $strUser "ExtensionAttribute6")))
$ArrLocal
Write-Host $ArrLocal[10][1]
Write-Host $ArrLocal[11][0]
}

This function is working well. inside the function  $ArrLocal[10][1]  return well the city and $ArrLocal[11][0] return well "Manager".


BUT, this is not working well

Function ManageArrAD()
{
      $ArrAD = [System.Collections.ArrayList]@()
        $ArrAD = PopulateArrAD
      Write-Host $ArrAD[10][1] DOES NOT RETURN CIty but 11
      Write-Host $ArrAD[11][0] return Runtime error...
}

Question : How to handle this ? How to have into a var of same type as created in PopulateArrAD into ManageArrAD ?

Thanks
Avatar of Dorababu M
Dorababu M
Flag of India image

Try by returning in your first function and give a try
return $ArrLocal

Open in new window


function firstFunction {
    $array = 1,2,3
    return $array
}

function secondFunction {
    $array = firstFunction
    Write-Host $array
}

Open in new window

Using return $ArrLocal is correct. Why? Because $ArrLocal.Add returns the index of the newly added item. If you do not explicitly return something, everything dumped into the pipeline is returned.

Of course this a very bad way to keep object information. You really should work with custom objects and their properties, or hash tables if you like that more. Arrays are for uniform information with usually unknown amount of "rows".
Using something like $usr.City or $usr['City'] is certainly much more appealing.
Avatar of Fr. Vandecan

ASKER

same behavior... not working.
If I convert your second array elements to strings containing the command
function PopulateArrAD
{
      $ArrLocal = [System.Collections.ArrayList]@() 

      [void] $ArrLocal.Add(("StreetAddress",'(GetItemFromAdUser $strUser "StreetAddress")'))
      [void] $ArrLocal.Add(("PostalCode",'(GetItemFromAdUser $strUser "PostalCode")'))
      [void] $ArrLocal.Add(("City",'(GetItemFromAdUser $strUser "City")'))
      [void] $ArrLocal.Add(("TelephoneNumber" ,'(GetItemFromAdUser $strUser "TelephoneNumber")'))
      [void] $ArrLocal.Add(("Fax",'(GetItemFromAdUser $strUser "Fax")'))
      [void] $ArrLocal.Add(("State", '(GetItemFromAdUser $strUser "State")'))
      [void] $ArrLocal.Add(("ADOU", '(GetOuUser $strUser)'))
      [void] $ArrLocal.Add(("SiteCode", '(GetCustomAttribute $strUser "ExtensionAttribute1")'))
      [void] $ArrLocal.Add(("City", '(GetItemFromAdUser $strUser "City")'))
      [void] $ArrLocal.Add(("Title", '(GetItemFromAdUser $strUser "Title")'))
      [void] $ArrLocal.Add(("Department", '(GetItemFromAdUser $strUser "Department")'))
      [void] $ArrLocal.Add(("Manager", '(GetItemFromAdUser $strUser "Manager")'))
      [void] $ArrLocal.Add(("Contrat_Nb",'(GetCustomAttribute $strUser "ExtensionAttribute6")'))
$ArrLocal
Write-Host $ArrLocal[10][1] 
Write-Host $ArrLocal[11][0]
}

Open in new window

your ManageArrAD function returns
(GetItemFromAdUser $strUser "Department")
Manager
 DOES NOT RETURN CIty but 11
11 return Runtime error...

Open in new window

with the first two lines coming from PopulateArrAD with correct results (as you stated), but the ManageArrAD output is different from what you told.
But nevertheless you are somehow correct, our recommendations do not change the output. I've explained why, but thought the intermediate results of the Add method would be dismissed if using return. That's not the case. You need to dismiss each unwanted return value. That is:
function PopulateArrAD
{
      $ArrLocal = [System.Collections.ArrayList]@() 

      $ArrLocal.Add(("StreetAddress",(GetItemFromAdUser $strUser "StreetAddress")))
      $ArrLocal.Add(("PostalCode",(GetItemFromAdUser $strUser "PostalCode")))
      $ArrLocal.Add(("City",(GetItemFromAdUser $strUser "City")))
      $ArrLocal.Add(("TelephoneNumber" ,(GetItemFromAdUser $strUser "TelephoneNumber")))
      $ArrLocal.Add(("Fax",(GetItemFromAdUser $strUser "Fax")))
      $ArrLocal.Add(("State", (GetItemFromAdUser $strUser "State")))
      $ArrLocal.Add(("ADOU", (GetOuUser $strUser)))
      $ArrLocal.Add(("SiteCode", (GetCustomAttribute $strUser "ExtensionAttribute1")))
      $ArrLocal.Add(("City", (GetItemFromAdUser $strUser "City")))
      $ArrLocal.Add(("Title", (GetItemFromAdUser $strUser "Title")))
      $ArrLocal.Add(("Department", (GetItemFromAdUser $strUser "Department")))
      $ArrLocal.Add(("Manager", (GetItemFromAdUser $strUser "Manager")))
      $ArrLocal.Add(("Contrat_Nb",(GetCustomAttribute $strUser "ExtensionAttribute6")))
Write-Host $ArrLocal[10][1] 
Write-Host $ArrLocal[11][0]
     return $ArrLocal
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
vey impressive and ... right... still thinking on vbscript.....
tks for this very valuable feedback !