天天看点

使用Powershell批量获取Exchange 2013邮箱用户容量使用量

         今天有客户要求需要获取邮箱用户的一些基本信息,其中一项是邮箱容量使用情况。需要使用Powershell来批量获取这些信息,于是乎我开始着手编写Powershell脚本。

1、脚本实现的功能

   脚本邮箱用户的脚本批量去获取邮箱用户的信息(显示名称、登录名、OU信息、邮箱配额、邮箱邮件数量、邮箱已使用大小、邮箱地址、邮箱容量使用情况等信息)

2、脚本运行环境

   目前该脚本可以用来获取Exchange 2010/2013。至于Exchange 2016目前还未进行测试。

3、修复了原脚本中的如下不足之处:

1)、修复显示名称中包含中文时显示乱码问题。

2)、添加了判断当用户邮箱设置了自定义配额且自定义配额设定为无限制时,脚本自动判断条件。

3)、添加了计算用户邮箱使用量(百分比)。

4)、添加了用户邮箱配额设定为数据库配额,且数据库配额设定为无限制时,脚本自动判断条件。

4、脚本内容:

#--------------------------------------------下面为脚本正文内容,直接复制如下内容,然后保存为.ps1-------------------------------------------------

Param(   

    [Parameter(Mandatory = $true)]    

    [string] $CSVPath    

)

$Mailboxes = Get-Mailbox -ResultSize Unlimited

$CSV = @()   

foreach($Mailbox in $Mailboxes)    

    {    

        $MailboxStats = (Get-MailboxStatistics $Mailbox -WarningAction SilentlyContinue )

        if ($mailbox.UseDatabaseQuotaDefaults -eq $true)   

            {    

                if((Get-MailboxDatabase $mailbox.Database).ProhibitSendReceiveQuota.Value -eq $null)    

                    {    

                        $ProhibitSendReceiveQuota=0    

                    }    

                if((Get-MailboxDatabase $mailbox.Database).ProhibitSendReceiveQuota.Value -ne $null)    

                       $ProhibitSendReceiveQuota=(Get-MailboxDatabase $mailbox.Database).ProhibitSendReceiveQuota.Value.ToMB()     

            }    

        if ($mailbox.UseDatabaseQuotaDefaults -eq $false)    

                if($mailbox.ProhibitSendReceiveQuota.Value -eq $null)    

                if($mailbox.ProhibitSendReceiveQuota.Value  -ne $null)    

                        $ProhibitSendReceiveQuota=$mailbox.ProhibitSendReceiveQuota.Value.ToMB()    

                    }                

            }

        $CSVLine = New-Object System.Object   

        $CSVLine | Add-Member -Type NoteProperty -Name "DisplayName" -Value $Mailbox.DisplayName    

        $CSVLine | Add-Member -Type NoteProperty -Name "UserName" -Value $Mailbox.SamAccountName    

        $CSVLine | Add-Member -Type NoteProperty -Name "PrimarySMTP" -Value $Mailbox.WindowsEmailAddress    

        $CSVLine | Add-Member -Type NoteProperty -Name "OrganizationalUnit" -Value $Mailbox.OrganizationalUnit    

        $CSVLine | Add-Member -Type NoteProperty -Name "EmailAliases" -Value ($Mailbox.EmailAddresses.SmtpAddress -join "; ")    

        if($MailboxStats)    

                if($ProhibitSendReceiveQuota -eq 0)    

                        $CSVLine | Add-Member -Type NoteProperty -Name "TotalItemSizeInMB" -Value $MailboxStats.TotalItemSize.Value.ToMB()    

                        $CSVLine | Add-Member -Type NoteProperty -Name "ItemCount" -Value $MailboxStats.ItemCount    

                        $CSVLine | Add-Member -Type NoteProperty -Name "StorageLimitStatus" -Value $Mailbox.StorageLimitStatus    

                        $CSVLine | Add-Member -Type NoteProperty -Name "UseDatabaseQuotaDefaults" -Value $Mailbox.UseDatabaseQuotaDefaults    

                        $CSVLine | Add-Member -Type NoteProperty -Name "ProhibitSendReceiveQuotaInMB" -Value "此用户无配额限制"    

                        $CSVLine | Add-Member -Type NoteProperty -Name '邮箱使用情况(%)' -Value "此用户无配额限制"    

                if($ProhibitSendReceiveQuota -ne 0)    

                         $CSVLine | Add-Member -Type NoteProperty -Name "TotalItemSizeInMB" -Value $MailboxStats.TotalItemSize.Value.ToMB()    

                         $CSVLine | Add-Member -Type NoteProperty -Name "ItemCount" -Value $MailboxStats.ItemCount    

                         $CSVLine | Add-Member -Type NoteProperty -Name "StorageLimitStatus" -Value $Mailbox.StorageLimitStatus    

                         $CSVLine | Add-Member -Type NoteProperty -Name "UseDatabaseQuotaDefaults" -Value $Mailbox.UseDatabaseQuotaDefaults    

                         $CSVLine | Add-Member -Type NoteProperty -Name "ProhibitSendReceiveQuotaInMB" -Value $ProhibitSendReceiveQuota    

                         $UsedSpace=[int]($MailboxStats.TotalItemSize.Value.ToMB()*100/$ProhibitSendReceiveQuota)    

                         $CSVLine | Add-Member -Type NoteProperty -Name '邮箱使用情况(%)' -Value ("$UsedSpace"+"%")    

             }    

        $CSV+=$CSVLine    

    }

$CSV | Sort TotalItemSize -Descending | Export-Csv -NoTypeInformation $CSVPath -Encoding Unicode

#-------------------------------------脚本内容结束------------------------------------------------------------------------------------------------------------------

5、脚本使用方法

       将脚本内容复制后另存为.ps1格式(例如:New-MailboxSizeReport-02.ps1。在Exchange 上使用Exchange Powershell执行该脚本,如图。

<a href="http://s3.51cto.com/wyfs02/M01/7F/7A/wKioL1cgT6Hi8BSYAAAMSMW1aXo606.png" target="_blank"></a>

     由于时间关系没有对脚本的执行效率进行优化,有兴趣的童鞋可以去研究研究。

本文转自 jialt 51CTO博客,原文链接:http://blog.51cto.com/jialt/1768198