PowerShell 2.0 Object formatting driving me crazy

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Stephen Owen Stephen Owen 2 years, 3 months ago.

  • Author
    Posts
  • #18708
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    Hi Guys,

    I'm running into an issue which is driving me crazy on an old server with PS 2.0 . I need to extract a lot of info about mailboxes for a friend named Mark (hence the terrible cmdlett name). Well, this 2.0 server is driving me crazy, mostly because I've forgotten how to make custom objects back in 2.0. It seems no matter what I do, I end up with a hashtable and not NoteProperties, and then my export gets all hosed.

    (blockquote)$Array = New-Object -TypeName System.Collections.ArrayList
    Function Get-MarkInfo {
    ForEach ($user in $users){
    $mbx = Get-Mailbox $user
    $mbxstat = Get-MailboxStatistics $user

    [PSCustomObject]$obj=@{Name=$user;RecipientType=$mbx.RecipientType;LastLoggedOnUserAccount=$mbxstat.LastLoggedOnUserAccount;LastLogOffTime=$mbxstat.LastLogOffTime;LastLogonTime=$mbxstat.LastLogonTime}

    $Array.Add($obj) | Out-Null
    }
    $Array | export-csv C:\temp\EmailExport.Csv
    }(/blockquote)

    Any Advice? I knew how to do this years ago but have since forgotten.

  • #18710
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    And I figured it out. Turns out PSCustomObject was a new big deal in Version 3.0, which I'd forgotten until now.

    I had to go all version 2.0 to get this to work.

    (pre)$Array = New-Object -TypeName System.Collections.ArrayList
    Function Get-MarkInfo {
    ForEach ($user in $users){
    $mbx = Get-Mailbox $user
    $mbxstat = Get-MailboxStatistics $user

    #[psobject]$obj=@{Name=$user;RecipientType=$mbx.RecipientType;LastLoggedOnUserAccount=$mbxstat.LastLoggedOnUserAccount;LastLogOffTime=$mbxstat.LastLogOffTime;LastLogonTime=$mbxstat.LastLogonTime}
    $ObjectProperties = @{
    Name = $user
    RecipientType=$mbx.RecipientType
    LastLoggedOnUserAccount=$mbxstat.LastLoggedOnUserAccount
    LastLogOffTime=$mbxstat.LastLogOffTime
    LastLogonTime=$mbxstat.LastLogonTime
    }

    $obj = New-Object PSObject -Property $ObjectProperties
    $obj | ft

    $Array.Add($obj) | Out-Null
    }
    $Array | ft
    }(/pre)

  • #18711
    Profile photo of Jason Hofferle
    Jason Hofferle
    Participant

    It's the [PSCustomObject] that's causing your problems. That syntax for new objects wasn't available in v2.

  • #18712
    Profile photo of Mike F Robbins
    Mike F Robbins
    Participant

    Looks like you figured it out, but I'll post this anyway:

    function Get-MarkInfo {
    foreach ($user in $users){
    $mbx = Get-Mailbox $user
    $mbxstat = Get-MailboxStatistics $user

    New-Object -TypeName PSObject -Property @{
    Name=$user
    RecipientType=$mbx.RecipientType
    LastLoggedOnUserAccount=$mbxstat.LastLoggedOnUserAccount
    LastLogOffTime=$mbxstat.LastLogOffTime
    LastLogonTime=$mbxstat.LastLogonTime
    }
    }
    }

    Get-MarkInfo | Export-Csv -Path C:\temp\EmailExport.Csv

  • #18713
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    Thank you guys, it was a blast from the past, using this approach to solve this problem.

    Thanks Mike, we ended up using a very similar solution here. I'll definitely blog about this one, to help any future PowerSheller's also having to delve into old systems.

You must be logged in to reply to this topic.