Powershell Exchange邮箱报告

时间:2015-04-06 21:22:39

标签: powershell exchange-server

我正在尝试为托管的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文件中的计数发送给他们。< / p>

我只是不确定如何一次性获得这些数据组合。

2 个答案:

答案 0 :(得分:0)

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

$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]属性:)

答案 1 :(得分:0)

如果我理解正确,答案很简单。这将输出csv内容。如果您愿意,可以删除引号并用逗号替换逗号,然后可以从PowerShell控制台复制/粘贴到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
}