【问题标题】:Powershell Exchange mailbox reportPowershell Exchange 邮箱报告
【发布时间】:2015-06-11 08:40:36
【问题描述】:

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

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

【问题讨论】:

    标签: powershell exchange-server


    【解决方案1】:

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

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

    【讨论】:

    • 这似乎是一个更好的方法......我不熟悉New-Object。但是无法让它运行。我得到一个“'Microsoft.Exchange.Diagnostics.ETraceConfiguration' 的类型初始化程序引发异常。类别信息:OperationStaopped,PSRemotingTransportException,FullyQualifiedErrorId:JobFailure”我确实认为也许我需要用“”OU“= $ou" 到 ""OU = $ou.Name" 也许,因为 $ou 变量将包含更多信息,我只需要 Name 值。仍然得到相同的结果。我正在运行 powershell 3。
    • 好的,不知道为什么,但我的 Powershell 窗口使用 PowerShell 3 时不断崩溃。但是我可以在装有 Powershell 2 的不同服务器上正常运行脚本。一个问题是上面的 foreach 循环每次运行时都会覆盖 $mailboxObject,因此最终 CSV 将在其中包含一行。所以我添加了一个“$mailboxList = @()”数组,然后在您的 New-Object 运行“$mailboxList += $mailboxCount”之后,然后在 $mailboxList 数组上执行 Export-CSV。这得到了所需的输出。感谢您的帮助!
    【解决方案2】:

    如果我的理解正确,答案很简单。这将输出 csv 内容。如果您愿意,可以删除引号并将逗号替换为 `t,然后您可以从 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
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 2012-11-26
      • 2017-08-01
      • 2015-07-28
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多