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

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to access and restore deleted Items (Recoverable Items) in the Exchange Online Mailbox dumpster with the Microsoft Graph API and PowerShell

As the information on how to do this would cover multiple posts, I've bound this into a series of mini post docs in my GitHub Repo to try and make this subject a little easier to understand and hopefully navigate for most people.   The Binder index is  https://gscales.github.io/Graph-Powershell-101-Binder/   The topics covered are How you can access the Recoverable Items Folders (and get the size of these folders)  How you can access and search for items in the Deletions and Purges Folders and also how you can Export an item to an Eml from that folder How you can Restore a Deleted Item back to the folder it was deleted from (using the Last Active Parent FolderId) and the sample script is located  https://github.com/gscales/Powershell-Scripts/blob/master/Graph101/Dumpster.ps1

Using the MSAL (Microsoft Authentication Library) in EWS with Office365

Last July Microsoft announced here they would be disabling basic authentication in EWS on October 13 2020 which is now a little over a year away. Given the amount of time that has passed since the announcement any line of business applications or third party applications that you use that had been using Basic authentication should have been modified or upgraded to support using oAuth. If this isn't the case the time to take action is now. When you need to migrate a .NET app or script you have using EWS and basic Authentication you have two Authentication libraries you can choose from ADAL - Azure AD Authentication Library (uses the v1 Azure AD Endpoint) MSAL - Microsoft Authentication Library (uses the v2 Microsoft Identity Platform Endpoint) the most common library you will come across in use is the ADAL libraries because its been around the longest, has good support across a number of languages and allows complex authentications scenarios with support for SAML etc. The
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.