【问题标题】:Best and quickest way to get exchange mailboxes count per database获取每个数据库的交换邮箱数的最佳和最快方法
【发布时间】:2021-12-31 20:16:15
【问题描述】:

我正在使用 Powershell 脚本为新员工自动创建活动目录帐户;其中一项任务是创建一个新的用户邮箱,并将其放入邮箱数量最少的数据库中;所以为了得到每个数据库的邮箱总数;一种选择是连接到交换和邮箱计数如下

Get-Mailbox -ResultSize Unlimited | Group-Object -Property:Database | Select-Object Name, Count | Sort-Object Name | Format-Table

但它有点慢,虽然我可以通过利用活动目录帐户的HomeMDB 属性并创建以下函数来获得更快的方法

function Get-MailboxPerDatabase(){     
    # get all AD account with excluding health and system mailboxes  
    $MBs = Get-ADUser -Filter {samaccountname -notlike "HealthMailbox*"} -Properties mail,homemdb | where {$_.mail -match "@" -and $_.samaccountname -notlike "SM_*"} | Group-Object Homemdb
    $Mailscount = foreach ($MB in $MBs){
        [string]$DB = $MB.Name
        [pscustomobject]@{
        Database = $DB.Substring(3,$DB.IndexOf(',CN')-3)
        Count = $MB.count
        }
    }
    $Mailscount | sort Count 
}

这给了我以下结果

Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
At line:4 char:9
        [pscustomobject]@{
         ~~~~~~~~~~~~~~~~~~
     CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
     FullyQualifiedErrorId : ArgumentOutOfRangeException
 

Database   Count
--------   -----
MGR1       330
DB3       330
DB2       330
MGR2       331
DB1       331
DB4       332

我的问题在这里:

  1. 为什么我无法获得 , (comma) 的索引,作为一种解决方法,我获得了 ,CN 的索引并返回 3 个步骤。

  2. 这是获取邮箱数量的有效方法还是我应该坚持交换正常方法。

编辑:我现在在修改

后设法解决了这个错误
    $MBs = Get-ADUser -Filter {samaccountname -notlike "HealthMailbox*"} -Properties mail,homemdb | where {$_.mail -match "@" -and $_.samaccountname -notlike "SM_*" -and $_.homemdb -match 'CN'} | Group-Object Homemdb

【问题讨论】:

    标签: powershell active-directory exchange-server


    【解决方案1】:

    适用于所有关注此帖子或寻找快速获取每个数据库邮箱数量的方法的人;

    这是最终函数的样子:

    function Get-MailboxPerDatabase(){     
        # get all AD account with excluding health and system mailboxes  
        $MBs = Get-ADUser -Filter {samaccountname -notlike "HealthMailbox*"} -Properties mail,homemdb | where {$_.mail -match "@" -and $_.samaccountname -notlike "SM_*" -and $_.homemdb -match 'CN'} | Group-Object Homemdb
        $Mailscount = foreach ($MB in $MBs){
            [string]$DB = $MB.Name
            [pscustomobject]@{
            Database = $DB.Substring(3,$DB.IndexOf(',')-3)
            Count = $MB.count
            }
        }
        $Mailscount | sort Count 
    }
    

    这个函数的执行时间在域控制器上只用了 2.3390401

    PS C:\Windows\System32> (Measure-Command -Expression {Get-MailboxPerDatabase}).TotalSeconds
    
    2.3390401
    

    使用Get-Mailbox 命令直接从交换服务器获取相同信息需要 59.96

    [PS] C:\Windows\system32>(Measure-Command -Expression { Get-Mailbox -ResultSize Unlimited | Group-Object -Property:Database | Select-Object Name, Count | Sort-Object Name | FT}).TotalSeconds
    
    59.9602879
    

    根据结果:

    在域控制器上使用Get-MailboxPerDatabase 函数

    Database   Count
    --------   -----
    MGR1      330
    DB3       330
    DB2       330
    MGR2      331
    DB1       331
    DB4       332
    

    在交换服务器上使用Get-Mailbox 命令

    Database   Count
    --------   -----
    MGR1      330
    DB3       330
    DB2       330
    MGR2      331
    DB1       331
    DB4       333
    

    我注意到数据库DB4 的结果不同,函数计数为332,但交换计数为333;检查结果后;我发现结果DiscoverySearchMailbox 中显示的交换不是我的函数Get-MailboxPerDatabase 返回的,这对我来说没问题,我不想包含它。

    根据关于indexof comma的问题,我发现没有问题,但问题来自substring方法,语法是 Substring( StartIndex [, length] ) 因此,如果我需要获取数据库的名称,我需要从结果中减去 3,例如 Database = $DB.Substring(3,$DB.IndexOf(',')-3)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多