PS Object data help

Hi Guys

i cant seem to pull the $user.TotalSizeMB correctly into the PSObject, can anyone see where i am going wrong here ?

$users = import-csv 'H:\EMEA Messaging\EV Data\Users2.csv'

$results = foreach ($user in $users)



{
   get-aduser $user.billingaccount -Properties employeeid, employeetype | select name, samaccountname, employeeid, employeetype

}



$outarray = @()

foreach ($result in $results)
{
    



$properties = @{Name = $result.Name
                SamAccountName = $result.samaccountname
                EmployeeID = $result.employeeid
                EmployeeType = $result.employeetype
                SpaceUsed = $user.TotalSizeMB
                                }

$Obj = New-Object -TypeName psobject -Property $properties
#Write-Output $obj 

$outarray += $obj 

}
$outarray | Out-GridView

CSV looks like below

BillingAccount TotalSizeMB

Mazzi.Binaisa 24,05,2017
Andrew.Roberts 01,10,1975
Kristin.Goudie 01,09,1975
KVS.Admin 01,04,3125
Lee.Partington 01,02,1975
David.Torres 01,01,9375
ben.davies 01,01,4375
Donal.McDaid 107558.3818
Mark.Anderson 84831.19824
Print.Room 79753.28125

View comes out displaying the space to be identical for all users

054760 Mazzi Binaisa 79753.28125 Ex-employee Mazzi.Binaisa
053173 Andrew Roberts 79753.28125 Andrew.Roberts
036507 Kristin Goudie 79753.28125 Ex-employee Kristin.Goudie
KVS.Admin 79753.28125 KVS.Admin
061081 Lee Partington 79753.28125 Ex-employee Lee.Partington
062809 David Torres 79753.28125 Ex-employee David.Torres
038214 Ben Davies 79753.28125 Ex-employee ben.davies
015465 Donal Mc Daid 79753.28125 Employee Donal.McDaid
015549 Mark Anderson 79753.28125 Employee Mark.Anderson
EML002 Print Room 79753.28125 Print.Room

figured it out, needed to push the get-user into a string

$users = import-csv 'H:\EMEA Messaging\EV Data\Users.csv'
$outarray = @()

$results = foreach ($user in $users)



{
   $stuff = get-aduser $user.billingaccount -Properties employeeid, employeetype | select name, samaccountname, employeeid, employeetype

   $properties = @{Name = $Stuff.Name
                SamAccountName = $stuff.samaccountname
                EmployeeID = $Stuff.employeeid
                EmployeeType = $Stuff.employeetype
                SpaceUsed = $user.TotalSizeMB
                                }

$Obj = New-Object -TypeName psobject -Property $properties
#Write-Output $obj 

$outarray += $obj 
}

$outarray |Out-GridView

Tips from “The Big Book of PowerShell Gotchas”:

https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/accumulating-output-in-a-function.html

"The problem here is that the function can generate multiple output objects, and the programmer is accumulating those into the $output variable. That means this function won't output anything until it's completely finished running. That isn't how PowerShell commands (and functions are commands) are usually meant to work. PowerShell commands should usually output each object to the pipeline, one at a time, as those objects are ready. That allows the pipeline to accumulate the output, and to immediately pass it along to whatever is next in the pipeline. That's how PowerShell commands are intended to work. Now, there are always exceptions. Sort-Object, for example, has to accumulate its output, because it can't actually sort anything until it has all of them. So it's called a _blocking command, _because it "blocks" the pipeline from doing anything else until it produces its output. But that's an exception.

It’s usually easy to fix this, by simply outputting to the pipeline instead of accumulating"

Thanks, & Thanks for the link to book :slight_smile:

This a cleaner way to build the PSObject and avoid the += and leverage calculated expressions for the SpaceUsed versus generating a new object:

$users = import-csv 'H:\EMEA Messaging\EV Data\Users.csv'

$results = foreach ($user in $users) {
   $stuff = get-aduser $user.billingaccount -Properties employeeid, employeetype | 
   Select name, 
          samaccountname, 
          employeeid, 
          employeetype,
          @{Name="SpaceUsed";Expression={$user.TotalSizeMB}}
}

$results |Out-GridView