Fr. Vandecan
asked on
Function returning an array - not working why ?
function PopulateArrAD
{
$ArrLocal = [System.Collections.ArrayL ist]@()
$ArrLocal.Add(("StreetAddr ess",(GetI temFromAdU ser $strUser "StreetAddress")))
$ArrLocal.Add(("PostalCode ",(GetItem FromAdUser $strUser "PostalCode")))
$ArrLocal.Add(("City",(Get ItemFromAd User $strUser "City")))
$ArrLocal.Add(("TelephoneN umber" ,(GetItemFromAdUser $strUser "TelephoneNumber")))
$ArrLocal.Add(("Fax",(GetI temFromAdU ser $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 ",(GetCust omAttribut e $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.ArrayL ist]@()
$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
{
$ArrLocal = [System.Collections.ArrayL
$ArrLocal.Add(("StreetAddr
$ArrLocal.Add(("PostalCode
$ArrLocal.Add(("City",(Get
$ArrLocal.Add(("TelephoneN
$ArrLocal.Add(("Fax",(GetI
$ArrLocal.Add(("State", (GetItemFromAdUser $strUser "State")))
$ArrLocal.Add(("ADOU", (GetOuUser $strUser)))
$ArrLocal.Add(("SiteCode",
$ArrLocal.Add(("City", (GetItemFromAdUser $strUser "City")))
$ArrLocal.Add(("Title", (GetItemFromAdUser $strUser "Title")))
$ArrLocal.Add(("Department
$ArrLocal.Add(("Manager", (GetItemFromAdUser $strUser "Manager")))
$ArrLocal.Add(("Contrat_Nb
$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.ArrayL
$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
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.
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.
ASKER
same behavior... not working.
If I convert your second array elements to strings containing the command
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]@()
[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]
}
your ManageArrAD function returns(GetItemFromAdUser $strUser "Department")
Manager
DOES NOT RETURN CIty but 11
11 return Runtime error...
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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vey impressive and ... right... still thinking on vbscript.....
tks for this very valuable feedback !
tks for this very valuable feedback !
Open in new window
Open in new window