![]() |
|
|||||||
| Notices |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
Strange behaviour of System.Object[]
Hello.
I've got a strange problem here and I can't figure out whats causing it. I’ve got this custom object: $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, Email; And a set that I’m adding my custom objects to: $objCustomerUsers = @(); I’ve got this little script: ///// BEGIN function getCustomerUsers { $objCustomerUsers = @(); ... foreach(...) { $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, Email; ... $objCustomerUsers = $objCustomerUsers + $objUser; } Write-Host $objCustomerUsers.Count; Write-Host $objCustomerUsers.GetType().FullName; return $objCustomerUsers; } $users = getCustomerUsers; Write-Host $users.Count; Write-Host $users.GetType().FullName; //// END This is ofcause not the whole script, but you get the idea. Now take a look at the output: 31 // $objCustomerUsers.Count; System.Object[] 46 // $users.Count; System.Object[] Why do I get $users.Count = 46 ? This should be 31 as it is inside the function, or am I doing something wrong? Thanks in advance. Tommy. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Strange behaviour of System.Object[]
> Now take a look at the output: > > 31 // $objCustomerUsers.Count; > System.Object[] > 46 // $users.Count; > System.Object[] > > > Why do I get $users.Count = 46 ? This should be 31 as it is inside the > function, or am I doing something wrong? > > Thanks in advance. > Tommy. You might have something else writing out from the function? What do these give: PS > $objCustomerUsers PS > $users Marco -- Microsoft MVP - Windows PowerShell PowerGadgets MVP Blog: |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Strange behaviour of System.Object[]
Hi Tommy,
This returns 33 fileinfo objects (on my computer) both inside and outside the function. I suspect there's something else in you function/script that causes the problem. function getCustomerUsers { $objCustomerUsers = @() foreach($file in get-childitem) { $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, Email $objCustomerUsers += $objUser } Write-Host $objCustomerUsers.Count Write-Host $objCustomerUsers.GetType().FullName $objCustomerUsers } $users = getCustomerUsers Write-Host $users.Count Write-Host $users.GetType().FullName --- Shay Levi $cript Fanatic TJ> Hello. TJ> TJ> I've got a strange problem here and I can't figure out whats causing TJ> it. TJ> TJ> I’ve got this custom object: TJ> $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, TJ> Email; TJ> And a set that I’m adding my custom objects to: TJ> $objCustomerUsers = @(); TJ> I’ve got this little script: TJ> TJ> ///// BEGIN TJ> TJ> function getCustomerUsers TJ> { TJ> $objCustomerUsers = @(); TJ> ... TJ> foreach(...) { TJ> $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, TJ> Email; TJ> ... TJ> $objCustomerUsers = $objCustomerUsers + $objUser; TJ> } TJ> Write-Host $objCustomerUsers.Count; TJ> Write-Host $objCustomerUsers.GetType().FullName; TJ> return $objCustomerUsers; TJ> } TJ> $users = getCustomerUsers; TJ> Write-Host $users.Count; TJ> Write-Host $users.GetType().FullName; TJ> //// END TJ> TJ> This is ofcause not the whole script, but you get the idea. TJ> TJ> Now take a look at the output: TJ> TJ> 31 // $objCustomerUsers.Count; TJ> System.Object[] TJ> 46 // $users.Count; TJ> System.Object[] TJ> Why do I get $users.Count = 46 ? This should be 31 as it is inside TJ> the function, or am I doing something wrong? TJ> TJ> Thanks in advance. TJ> Tommy. |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: Strange behaviour of System.Object[]
Marco Shaw [MVP] wrote:
> >> Now take a look at the output: >> >> 31 // $objCustomerUsers.Count; >> System.Object[] >> 46 // $users.Count; >> System.Object[] >> >> >> Why do I get $users.Count = 46 ? This should be 31 as it is inside >> the function, or am I doing something wrong? >> >> Thanks in advance. >> Tommy. > > You might have something else writing out from the function? > > What do these give: > PS > $objCustomerUsers > PS > $users > > Marco > Hi Marco This got me more confused. $objCustomerUsers returns an empty line. $users returns the following: 0 1 2 3 CN=xx,OU=xx,OU=xx,DC=xx,DC=xx,DC=xx0 1 2 3 CN=xx,OU=xx,OU=xx,DC=xx,DC=xx,DC=xx0 1 2 3 Including lots of empty spaces afterwads. the correct LDAP string has been replaced with xx. If you wan't to see the whole source code, please take a look at this url: |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: Strange behaviour of System.Object[]
Shay Levi skrev:
> Hi Tommy, > > > This returns 33 fileinfo objects (on my computer) both inside and > outside the function. I suspect there's something else > in you function/script that causes the problem. > > > function getCustomerUsers > { > $objCustomerUsers = @() > > foreach($file in get-childitem) { > $objUser = "" | Select-Object CustomerID, ID, Name, Telephone, > $objCustomerUsers += $objUser > } > Write-Host $objCustomerUsers.Count > Write-Host $objCustomerUsers.GetType().FullName > $objCustomerUsers > } > > $users = getCustomerUsers > Write-Host $users.Count > Write-Host $users.GetType().FullName > > > > > > --- > Shay Levi > $cript Fanatic > > Hi Shay, Thanks for your reply. Seems strange. I might have done something totally wrong in my script then. I've posted it at - Tommy |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Re: Strange behaviour of System.Object[]
Tommy Holm Jakobsen <> wrote in news:#yiZobg1IHA.2208
@TK2MSFTNGP04.phx.gbl: > $objCustomerUsers returns an empty line. > $users returns the following: > 0 1 2 3 CN=xx,OU=xx,OU=xx,DC=xx,DC=xx,DC=xx0 1 2 3 > CN=xx,OU=xx,OU=xx,DC=xx,DC=xx,DC=xx0 1 2 3 > Including lots of empty spaces afterwads. the correct LDAP string has > been replaced with xx. > > If you wan't to see the whole source code, please take a look at this > url: Those numbers you see in $users are return codes from $objSearcher.PropertiesToLoad.Add(). It returns the index of item you just added. PowerShell places indexes to pipe and function returns them (in addition to what you return with return statement). Use [void] whenever you call Add method: [void] $objSearcher.PropertiesToLoad.Add("name") |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |