我有一段 Powershell 代码,它从一个 OrganisationalUnit 中获取邮箱统计信息,但我如何才能从另外两个 OrganisationalUnit 中获取它,而无需再次单独运行代码。如何在 forloop 中创建 OU 数组?
另外,我该如何对结果进行排序,以便将使用率最高的结果显示在顶部?
$(Foreach ($mailbox in Get-Recipient -ResultSize Unlimited -OrganizationalUnit
"Users" -RecipientType UserMailbox){
$Stat = $mailbox | Get-MailboxStatistics | Select TotalItemSize,ItemCount
New-Object PSObject -Property @{
FirstName = $mailbox.FirstName
LastName = $mailbox.LastName
DisplayName = $mailbox.DisplayName
TotalItemSize = $Stat.TotalItemSize
ItemCount = $Stat.ItemCount
PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
Alias = $mailbox.Alias
}
}) | Select FirstName,LastName,DisplayName,TotalItemSize,ItemCount,PrimarySmtpAddress,Alias | Export-CSV e:\MailboxSizeReport.csv -NTI
答案1
这是修复的尝试,但由于我没有您的环境,因此无法测试。
我添加了一个 OU 数组,并执行 ForEach 来检索 OU 名称,然后将它们传递给您的代码。顺便说一句,我在 Export-CSV 中使用了 -append,这需要 Powershell V3,如果您仍在运行 V2,请告诉我。
$OUs = @("users","managers","admins","VIPs","rest of the world")
Foreach ($OU in $OUs)
{
$(Foreach ($mailbox in Get-Recipient -ResultSize Unlimited -OrganizationalUnit $OU -RecipientType UserMailbox)
{
$Stat = $mailbox | Get-MailboxStatistics | Select TotalItemSize,ItemCount
New-Object PSObject -Property @{
FirstName = $mailbox.FirstName
LastName = $mailbox.LastName
DisplayName = $mailbox.DisplayName
TotalItemSize = $Stat.TotalItemSize
ItemCount = $Stat.ItemCount
PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
Alias = $mailbox.Alias
}
}) | Select FirstName,LastName,DisplayName,TotalItemSize,ItemCount,PrimarySmtpAddress,Alias | Export-CSV e:\MailboxSizeReport.csv -NTI -append
}