Wednesday, February 29, 2012

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  

No comments: