1

I need to create a report for exchange server 2010.

Where I need the users Display name, lastlogontime and account status ie enabled or disable.

get-mailbox statistics shows lastlogon and get-user can show account control status.

So I tried this not working anyhow.

Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ }
get-mailboxstatistics -server 00-exchbx01 |
  ForEach {
    New-Object psobject |
    Add-Member -PassThru NoteProperty name $_.name |
    Add-Member -PassThru NoteProperty lastlogontime $_.lastlogontime |
    Add-Member -PassThru NoteProperty UserAccountControl $Users[$_.SamAccountName].UserAccountControl
  } |select name,lastlogontime,useraccountcontrol |sort-lastlogontime -descending | export-csv c:\ussersxx.csv -nti

also tried No luck Yet any Help?

Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ } | get-mailboxstatistics -server 00-exchbx01  | select Name,useraccountcontrol, lastlogontime|sort-lastlogontime -descending | Export-csv c:\report.csv
`

2 Answers2

0

There's a couple of things here, most of that looks fine except for your last line:

 ||sort-lastlogontime -descending |

This should become:

 | sort -property lastlogontime -descending |

What you'll also run into is that you might end up with things coming down the pipeline from Get-MailboxStatistics that aren't in your $Users hash table. This will results in getting errors when using your hash table to fill out a property the psobject.

You could adjust this to iterate through the hash table, passing the SamAccountName as the value for the Identity property (but this will likely be slower), or add in some error handling so that you just end up with an empty property rather than it completely erroring out.

Windos
  • 1,796
  • 1
  • 13
  • 19
  • 1
    You can ingore that minor mistakes I am away from server and typing it here "Get-MailboxStatistics that aren't in your $Users" may be you are right here the error was identity null.Looks like i need to pull users from ad and new pso object – rash Willee Jan 06 '17 at 03:57
  • @rashWillee given how good the rest of it was, I assumed that must have been a rush job/suedo code esque thing. My personal preference when doing this sort of thing is to pull the user objects from AD (mainly because I do a lot of filtering based on OU or other AD attributes), then fetch the Exchange info I need from that collection. – Windos Jan 06 '17 at 04:00
  • + all users and not enabled with mailbox – rash Willee Jan 06 '17 at 17:53
0

This should do it.

$outtbl = @()
$users = Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ }
$users | % {
  $x = Get-MailboxStatistics $_ | Select LastLogonTime
  $t = New-Object PSObject -Property @{
    Name = $_.Name
    LastLogonTime = $x.LastLogontime
    UserAccountControl = $_.UserAccountControl
  }
  $outtbl += $t
}

This is the way I've always done these "combining results of different commands" type scenarios.

You can then do $outtbl | Sort LastLogonTime -Descending | Export-Csv c:\ussersxx.csv -nti to export the data