Somebody asked a question the other week about getting all the Folder Sizes via EWS which you can do easily using the PR_MESSAGE_SIZE_EXTENDED property and FindFolders operation (you can also get the folder size's using the Exchange Management Shell via Get-MailboxFolderStatistics cmdlet). But there is some other interesting stuff you can get via EWS that you can't from the EMS cmdlet such as the Default FolderClass (eg the PR_CONTAINER_CLASS_W http://msdn.microsoft.com/en-us/library/office/cc839839(v=office.15).aspx) which will tell you what items are being stored in that particular Folder (although as documented it's not a mandatory property although its absence in the past has caused problem in OWA etc). Another property that looks interesting but doesn't seem to be well documented is the PR_ATTACH_ON_NORMAL_MSG_COUNT which appears to the be the total number of attachments on messages in that folder including seemly inline attachment (Note I can't confirm this as it doesn't appear to be documented and can only give anecdotal test results so do your own testing if your interested in this).
So with all these interesting properties you can put together a different type of Mailbox statistics script that will grab Folder stats by FolderClass and show the following info about a mailbox
I've put a download of this script here to run this script enter the EmailAddress of the mailbox you want to run it against and It will will output a CSV to the c:\temp directory the code itself looks like
So with all these interesting properties you can put together a different type of Mailbox statistics script that will grab Folder stats by FolderClass and show the following info about a mailbox
- ## Get the Mailbox to Access from the 1st commandline argument
- $MailboxName = $args[0]
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $psCred = Get-Credential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- function ConvertToString($ipInputString){
- $Val1Text = ""
- for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){
- $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))
- $clInt++
- }
- return $Val1Text
- }
- $FolderClassrpt = @{}
- function GetFolderSizes{
- param (
- $rootFolderId = "$( throw 'rootFolderId is a mandatory Parameter' )"
- )
- process{
- #Define Extended properties
- $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
- $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);
- $folderidcnt = $rootFolderId
- #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling
- $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
- #Deep Transval will ensure all folders in the search path are returned
- $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
- $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
- $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
- $PR_ATTACH_ON_NORMAL_MSG_COUNT = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x66B1, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);
- #Add Properties to the Property Set
- $psPropertySet.Add($PR_Folder_Path);
- $psPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED)
- $psPropertySet.Add($PR_ATTACH_ON_NORMAL_MSG_COUNT)
- $fvFolderView.PropertySet = $psPropertySet;
- #The Search filter will exclude any Search Folders
- $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
- $fiResult = $null
- #The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox
- do {
- $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)
- foreach($ffFolder in $fiResult.Folders){
- $foldpathval = $null
- #Try to get the FolderPath Value and then covert it to a usable String
- if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))
- {
- $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)
- $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }
- $hexString = $hexArr -join ''
- $hexString = $hexString.Replace("FEFF", "5C00")
- $fpath = ConvertToString($hexString)
- }
- $folderSize = $null
- [Void]$ffFolder.TryGetProperty($PR_MESSAGE_SIZE_EXTENDED,[ref] $folderSize)
- [Int64]$attachcnt = 0
- [Void]$ffFolder.TryGetProperty($PR_ATTACH_ON_NORMAL_MSG_COUNT,[ref] $attachcnt)
- if($attachcnt -eq $null){
- $attachcnt = 0
- }
- "FolderPath : " + $fpath + " : " + $folderSize
- $fldClass = $ffFolder.FolderClass
- if($fldClass -eq $null){$fldClass = "IPF.Note"}
- if($FolderClassrpt.ContainsKey($fldClass)){
- $FolderClassrpt[$fldClass].NumberOfFolders += 1
- $FolderClassrpt[$fldClass].AttachOnMsgCount += $attachcnt
- $FolderClassrpt[$fldClass].ItemSize += [Int64]$folderSize
- $FolderClassrpt[$fldClass].ItemCount += [Int64]$ffFolder.TotalCount
- }
- else{
- $rptObj = "" | select FolderClass,NumberOfFolders,AttachOnMsgCount,ItemSize,ItemCount
- $rptObj.FolderClass = $fldClass
- $FolderClassrpt[$fldClass].NumberOfFolders
- $rptObj.ItemSize = [Int64]$folderSize
- $rptObj.ItemCount = [Int64]$ffFolder.TotalCount
- $rptObj.AttachOnMsgCount += $attachcnt
- $rptObj.NumberOfFolders = 1
- $FolderClassrpt.Add($fldClass,$rptObj)
- }
- }
- $fvFolderView.Offset += $fiResult.Folders.Count
- }while($fiResult.MoreAvailable -eq $true)
- }
- }
- GetFolderSizes -rootFolderId (new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName))
- $FolderClassrpt.Values | select FolderClass,NumberOfFolders,AttachOnMsgCount,ItemCount,@{label="TotalSize(MB)";expression={[math]::Round($_.ItemSize/1MB,2)}} | export-csv c:\temp\$MailboxName-fldsizebyclass.csv -NoTypeInformation