Powershell获取Exchange邮箱用户配额

    客户场景:为很多用户单独配置过邮箱配额,想重新规划,现需要导致所有用户实际容量清单
    实现方式:直接上代码

#加载Exchange命令
Add-PSSnapin Microsoft.ex*
#主命令
[array]$output = "" | Select Name,ProhibitSendQuota,DBName
$DBs = Get-MailboxDatabase | Select Name,ProhibitSendQuota
foreach ($DB in $DBs) {
[string]$DBQuota = $DB.ProhibitSendQuota
[string]$DBName = $DB.Name
Write-Host "Database $DBName's quota is $DBQuota"
$Mails = Get-Mailbox -Database $DBName | Select Name,ProhibitSendQuota
foreach ($Mail in $Mails) {
if ($Mail.ProhibitSendQuota -eq "Unlimited") {
[string]$MailQuota = $DBQuota
}
else {
[string]$MailQuota = $Mail.ProhibitSendQuota
}

专注于为中小企业提供成都网站设计、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业昌乐免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

$Temp = "" | Select Name,ProhibitSendQuota,DBName
$Temp.Name = $Mail.Name
$Temp.ProhibitSendQuota = $MailQuota
$Temp.DBName = $DBName
#$Temp

$output += $Temp

}
}
$output | Export-Csv MailQuota.csv -NoTypeInformation -Encoding UTF8
输出结果:
Powershell获取Exchange 邮箱用户配额
与预期对比:
Powershell获取Exchange 邮箱用户配额
结论:如果为用户配置了不限制容量,则不能正常获取(客户实际环境中仅为董事长有配置无限制,手动修正,符合客户需求),欢迎帮解决此bug

    Update:
    客户提出了几点疑问:
    1. 这个结果我无法看出哪些用户是有手动配置过的
    2. 我不知道用户使用了多少,用了多少空间
    3. 需要最后登录时间
    4. 我有很多Mail Server,如果我只想获取一台怎么办?
    5. 我在C盘打开的这个脚本,但这个脚本实际位置在D盘,这个输出文件输出到了C盘怎么办?需要输出到脚本所在的目录

针对以上,优化了脚本:

  1. 先直接上脚本
    param($Server)
    $outfile = $(Split-Path -Parent $MyInvocation.MyCommand.Definition) + "\" + (Get-Date -UFormat "%Y-%m-%d_%H-%M-%S") + ".csv"
    Write-Host $outfile
    [array]$output = "" | Select Name,DBName,UserQuota,DBQuota,ProhibitSendQuota,TotalItemSize,Use,LastLogonTime
    #Select Name,DBName,UserQuota,DBQuota,ProhibitSendQuota,TotalItemSize,Use
    Add-PSSnapin Microsoft.ex
    if($Server){
    $DBs = Get-MailboxDatabase -Server $Server| Select Name,ProhibitSendQuota
    } else {
    $DBs = Get-MailboxDatabase | Select Name,ProhibitSendQuota
    }
    foreach ($DB in $DBs) {
    [string]$DBQuota = $DB.ProhibitSendQuota
    [string]$DBName = $DB.Name
    Write-Host "Database $DBName's quota is $DBQuota"
    $Mails = Get-Mailbox -Database $DBName -ResultSize unlimited | Select SamaccountName,ProhibitSendQuota
    foreach ($Mail in $Mails) {
    if ($Mail.ProhibitSendQuota -eq "Unlimited") {
    [string]$MailQuota = $DBQuota
    }
    else {
    [string]$MailQuota = $Mail.ProhibitSendQuota
    }
    $TotalItemSize = Get-MailboxStatistics -Identity $Mail.SamaccountName | select TotalItemSize,LastLogonTime
    $TempA = $Mail.ProhibitSendQuota
    $Temp = "" | Select Name,DBName,UserQuota,DBQuota,ProhibitSendQuota,TotalItemSize,Use,LastLogonTime
    $Temp.Name = $Mail.SamaccountName
    $Temp.DBName = $DBName
    if ($TempA -eq "Unlimited") {
    $Temp.UserQuota = $TempA
    } else {
    #$Temp.UserQuota = $TempA.replace(" ","").split("(")[0]
    $Temp.UserQuota = $($TempA -replace " ").Split("(")[0]
    }
    $Temp.DBquota = $($DBQuota -replace " ").Split("(")[0]
    $Temp.ProhibitSendQuota = $($MailQuota -replace " ").Split("(")[0]
    $Temp.TotalItemSize = $($TotalItemSize.TotalItemSize -replace " ").Split("(")[0]
    $Temp.Use = ('{0:n2}' -f $($Temp.TotalItemSize / $Temp.ProhibitSendQuota
    100)) +'%'
    $Temp.LastLogonTime = $TotalItemSize.LastLogonTime
    #$Temp

$output += $Temp

}
}
#$output[1..$output.count]
$output[1..$output.count] | Export-Csv $outfile -NoTypeInformation -Encoding UTF8 -Force

  1. 输出结果
    Powershell获取Exchange 邮箱用户配额
  2. 脚本执扫方式
    a. 直接执行则获取所有用户
    b. .\xxx.ps1 -Server XX则获取指定Server上的DB

欢迎补充


文章标题:Powershell获取Exchange邮箱用户配额
当前URL:http://pwwzsj.com/article/pdiisp.html