Powershell Exchange邮箱报告

Powershell Exchange mailbox report

我正在尝试为我们托管的 Exchange 平台生成邮箱报告并稍微自动化一些。基本上每个租户都在一个 OU 中。所以我试图首先拉出一个 OU 列表,然后计算每个 OU 中的邮箱。这是我目前拥有的:

$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })

foreach ($ou in $ous) {
(Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count
}

它有效……有点。结果将是每行上的大量数字,每个 OU 的邮箱计数。问题是,然后我在 $ous 变量中有 OU,并且我正在将计数输出到屏幕。我需要的是输出两列,即 OU,以及另一列中的计数,这样我就可以将其通过管道传输到 Export-CSV cmdlet 中,这样我就可以将客户端名称 (OU) 和 CSV 文件中的计数通过电子邮件发送给他们。

我只是不确定如何一次获取所有数据组合。

组织信息的最简单方法是将其放入一个对象中,如果您稍后需要更改数据,它已经在一个您可以操作的对象中。

$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })

foreach ($ou in $ous)
{
    # This is how many mailboxes this OU has
    $mailboxCount = (Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count

    # This is a fancy object to store our information
    $mailboxObject = New-Object psobject -Property ([Ordered]`
    @{
        "OU" = $ou
        "MailboxCount" = $mailboxCount
    })

    # You can export your CSV here or do other stuff
    # $mailboxObject | Export-CSV C:\MyPath -NoTypeInformation
}

小提示:如果您不使用 PowerShell v3,请删除 [Ordered] 属性 :)

如果我没理解错的话,答案很简单。这将输出 csv 内容。如果您愿意,可以删除引号并将逗号替换为 `t,然后您可以从 PowerShell 控制台 copy/paste 到 Excel.

$ous = (Get-ADOrganizationalUnit -SearchBase "OU=Microsoft Exchange Hosted Organizations,DC=yomamma,DC=com" -Filter { (ObjectClass -eq 'organizationalunit') -and (Name -notlike 'Hosted Organization Security Groups') -and (Name -Notlike 'Microsoft Exchange Hosted Organizations') })

foreach ($ou in $ous) {
    '"' + $ou.Name + '",' + (Get-Mailbox -Organization $ou.Name -Filter {( Name -notlike 'Administrator' -and Name -notlike 'DiscoverySearch*' )} -ResultSize unlimited).count
}