Author Posts

September 10, 2014 at 9:55 am

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.

September 10, 2014 at 10:10 am

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)

September 10, 2014 at 10:10 am

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

September 10, 2014 at 10:19 am

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

September 10, 2014 at 10:30 am

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.