天天看點

使用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