PS Object data help

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 months, 3 weeks ago.

  • Author
    Posts
  • #67959
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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

  • #67966
    Profile photo of Mark Prior
    Mark Prior
    Participant

    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
    
    
  • #67980
    Profile photo of Hermann
    Hermann
    Participant

    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"

  • #67984
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Thanks, & Thanks for the link to book 🙂

  • #67986
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    

You must be logged in to reply to this topic.