Skip to main content

HowTo Series Sample 2 - Accessing System Public Folders : OAB Stats script

The following script demonstrates how you can access System Level Public Folders (Non_IPM_Subtree) using the EWS Managed API and shows how to report on the status of the OAB (Offline Address Book) distribution folders. (Note on 2010 you can also do this with the Exchange Management Shell). It produces a CSV report like



The main script is based around the how to series template but because there is no enumeration to get to the Non_IPM_Subtree one trick is needed. The first thing is to bind to the normal Public Folder Root using PublicFoldersRoot Enum then use the ParentFolderId property of this folder to bind to the parent then search  for the Non_IPM_Subtree folder which will be a subfolder of this folder. Then you can query for any System Folder from this point in this example it finds the OAB folder queries for any subfolders under this folder and then finds any of these subfolders with Items and queries the size, number of Items and the Modified date of the last item.

I've put a download of this script here the script itself looks like

  1. ## EWS Managed API Connect Script  
  2. ## Requires the EWS Managed API and Powershell V2.0 or greator    
  3.     
  4. ## Load Managed API dll    
  5. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"    
  6.     
  7. ## Set Exchange Version    
  8. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1    
  9.     
  10. ## Create Exchange Service Object    
  11. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  12.     
  13. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  14.     
  15. #Credentials Option 1 using UPN for the windows Account    
  16. $creds = New-Object System.Net.NetworkCredential("user@domain.com","password")     
  17. $service.Credentials = $creds        
  18.     
  19. #Credentials Option 2    
  20. #service.UseDefaultCredentials = $true    
  21.     
  22. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  23.     
  24. [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}    
  25.     
  26. ## 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    
  27.     
  28. #CAS URL Option 1 Autodiscover    
  29. $service.AutodiscoverUrl("email@domain.com",{$true})    
  30. "Using CAS Server : " + $Service.url     
  31.      
  32. #CAS URL Option 2 Hardcoded    
  33.     
  34. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  35. #$service.Url = $uri      
  36.     
  37. ## Optional section for Exchange Impersonation    
  38.     
  39. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, "email@domain.com")    
  40.   
  41.   
  42. #Define the FolderSize Extended Property  
  43. $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  44. $Propset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)  
  45. $Propset.add($PR_MESSAGE_SIZE_EXTENDED)  
  46.   
  47. $PFRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)  
  48. $NonIPMPfRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$PFRoot.ParentFolderId)  
  49. $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)  
  50. $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"NON_IPM_SUBTREE")  
  51. $folders = $NonIPMPfRoot.Findfolders($sfSearchFilter,$fvFolderView)  
  52. foreach($folder in $folders.Folders){  
  53.     #$folder   
  54.     $sfSearchFilter1 = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"OFFLINE ADDRESS BOOK")  
  55.     $fvFolderView1 =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)  
  56.     $fvFolderView1.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;  
  57.     $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
  58.     $OABFolder = $folder.Findfolders($sfSearchFilter1,$fvFolderView1).Folders[0]  
  59.     $OABFolders = $OABFolder.Findfolders($fvFolderView1)  
  60.     foreach($OABSubFolder in $OABFolders.Folders){  
  61.         if($OABSubFolder.ChildFolderCount -gt 0){  
  62.             $OABSubFolder.DisplayName  
  63.             $fvFolderView2 =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)  
  64.             $fvFolderView2.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;  
  65.             $fvFolderView2.PropertySet = $Propset  
  66.             $SubFolders = $OABSubFolder.Findfolders($fvFolderView2)  
  67.                 foreach($SubFolder in $SubFolders.Folders){  
  68.                 $rptObj = "" | select  RootFolderName,SubFolderName,FolderItemCount,FolderSize,NewestItemLastModified  
  69.                 $rptObj.RootFolderName = $OABSubFolder.DisplayName  
  70.                 $rptObj.SubFolderName = $SubFolder.DisplayName  
  71.                 $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
  72.                 $FindItems = $SubFolder.FindItems($ivItemView)  
  73.                 $rptObj.FolderItemCount = $FindItems.Items.Count  
  74.                 if($FindItems.Items.Count -gt 0){  
  75.                     $rptObj.NewestItemLastModified = $FindItems.Items[0].LastModifiedTime.ToString()  
  76.                 }  
  77.                 $folderSize = $null  
  78.                 if($SubFolder.TryGetProperty($PR_MESSAGE_SIZE_EXTENDED, [ref]$folderSize)){  
  79.                     $rptObj.FolderSize = [MATH]::Round($folderSize/1024,0)  
  80.                 }  
  81.                       
  82.                 $rptCollection += $rptObj  
  83.             }  
  84.   
  85.         }  
  86.           
  87.   
  88.     }  
  89. }  
  90. $rptCollection | Export-Csv -NoTypeInformation c:\temp\OabStatReports.csv  

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Exporting and Uploading Mailbox Items using Exchange Web Services using the new ExportItems and UploadItems operations in Exchange 2010 SP1

Two new EWS Operations ExportItems and UploadItems where introduced in Exchange 2010 SP1 that allowed you to do a number of useful things that where previously not possible using Exchange Web Services. Any object that Exchange stores is basically a collection of properties for example a message object is a collection of Message properties, Recipient properties and Attachment properties with a few meta properties that describe the underlying storage thrown in. Normally when using EWS you can access these properties in a number of a ways eg one example is using the strongly type objects such as emailmessage that presents the underlying properties in an intuitive way that's easy to use. Another way is using Extended Properties to access the underlying properties directly. However previously in EWS there was no method to access every property of a message hence there is no way to export or import an item and maintain full fidelity of every property on that item (you could export the...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.