结合 get-user get-mailboxstatistics exchange 2010

combine get-user get-mailboxstatistics exchange 2010

我需要为 Exchange Server 2010 创建报告。

我需要用户显示名称、上次登录时间和帐户状态,即启用或禁用。

get-mailbox 统计显示 lastlogon 和 get-user 可以显示帐户控制状态。

所以我试了这个不管怎样都不行。

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

也试过了 没有运气 有帮助吗?

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
`

这里有一些东西,除了你的最后一行,大部分看起来都不错:

 ||sort-lastlogontime -descending |

这应该变成:

 | sort -property lastlogontime -descending |

您还将 运行 了解的是,您最终可能会从 Get-MailboxStatistics 的管道中得到一些不在您的 $Users 哈希中的东西 table.这将导致在使用哈希 table 填写 属性 psobject 时出错。

可以调整它以遍历哈希 table,将 SamAccountName 作为身份 属性 的值传递(但这可能是更慢),或者添加一些错误处理,这样你就可以得到一个空的 属性 而不是完全出错。

这应该可以做到。

$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
}

这就是我处理这些 "combining results of different commands" 类型场景的方式。

然后您可以$outtbl | Sort LastLogonTime -Descending | Export-Csv c:\ussersxx.csv -nti导出数据