【发布时间】: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
我的问题在这里:
-
为什么我无法获得
,(comma) 的索引,作为一种解决方法,我获得了,CN的索引并返回 3 个步骤。 -
这是获取邮箱数量的有效方法还是我应该坚持交换正常方法。
编辑:我现在在修改
后设法解决了这个错误 $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