In this series so far I've covered a lot of ground in EWS covering all the everyday operations so its time now to look at some of the more interesting and useful things you can do. Folder permissions are one thing that can both affect the code you run if you don't have the correct or enough rights in a folder your accessing and are also something you may want to manage using EWS. As before in this series I should point out that folder permission can be managed within the Exchange Management Shell using the Get and Set-MailboxFolderPermissions cmdlets and this can be a better solution for managing permissions.
Exchange Folder permission in a netshell
Exchange uses the normal discretionary access control list (DACL) with Access Control Entries (ACE's) to control access to its resources but there are a few special things to keep in mind. (There are also SACL's on public folders which you can't set from EWS).
Special ACE's in the Folder DACL : There are two special Group ACE's in a Exchange DACL there is the Default ACE (basically every authenticated mailbox user) and the Anoymous ACE (more for public folders which im not really going to dicuss).
FreeBusy Permissions : In Exchange 2007 freebusy rights where introduced see http://blogs.msdn.com/b/stephen_griffin/archive/2007/05/25/new-freebusy-rights-in-exchange-outlook-2007.aspx . So if your accessing the calendar folder in a users mailbox in EWS you will use a different class to accommodate these extra free-busy permissions.
Let's start by looking at some samples, to access the permission on a users Inbox you can use the following
To show the FreeBusy Permissions on the Calendar Folder
Modifying Folder Permissions
When you want to modify folder permission using EWS you first need to get the existing ACL from the folder check to see if there is an existing ACE for the user you want to add/modify and either change the existing ACE's permission or delete it and add a new ACE (what you are trying to avoid is duplicating the ACE which will cause an error). Here is an example of adding reviewer rights for a specific user to the Inbox in this code if it detects an existing ACE it just removes that ACE and adds a new one with reviewer rights.
Special considerations for the Calendar Folder
When working with calendar folder permissions you should also make sure you mirror any changes that you make to the Freebusy folder in the Non_IPM_Subtree if you don't do this you may find that you cant modify appointments even though you have rights in the folder. Eg the following is an example of giving the Default ACE (which essentially means everybody) editor access to a calendar.
Reporting on all Shared folders in a Mailbox
Here's a full script that will enumerate every folder in a mailbox and check all the ACE's and report on any folders that are shared.
Exchange Folder permission in a netshell
Exchange uses the normal discretionary access control list (DACL) with Access Control Entries (ACE's) to control access to its resources but there are a few special things to keep in mind. (There are also SACL's on public folders which you can't set from EWS).
Special ACE's in the Folder DACL : There are two special Group ACE's in a Exchange DACL there is the Default ACE (basically every authenticated mailbox user) and the Anoymous ACE (more for public folders which im not really going to dicuss).
FreeBusy Permissions : In Exchange 2007 freebusy rights where introduced see http://blogs.msdn.com/b/stephen_griffin/archive/2007/05/25/new-freebusy-rights-in-exchange-outlook-2007.aspx . So if your accessing the calendar folder in a users mailbox in EWS you will use a different class to accommodate these extra free-busy permissions.
Let's start by looking at some samples, to access the permission on a users Inbox you can use the following
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
- $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- foreach($Permission in $Inbox.Permissions){
- if($Permission.UserId.StandardUser -eq $null){
- "User : " + $Permission.UserId.PrimarySmtpAddress
- }
- else{
- "User : " + $Permission.UserId.StandardUser.ToString()
- }
- $Permission
- }
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
- $Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- foreach($Permission in $Calendar.Permissions){
- $rptObj = "" | Select User,FreeBusyRights
- if($Permission.UserId.StandardUser -eq $null){
- $rptObj.User = $Permission.UserId.PrimarySmtpAddress
- $rptObj.FreeBusyRights = $Permission.ReadItems
- }
- else{
- $rptObj.User = $Permission.UserId.StandardUser.ToString()
- $rptObj.FreeBusyRights = $Permission.ReadItems
- }
- $rptObj
- }
When you want to modify folder permission using EWS you first need to get the existing ACL from the folder check to see if there is an existing ACE for the user you want to add/modify and either change the existing ACE's permission or delete it and add a new ACE (what you are trying to avoid is duplicating the ACE which will cause an error). Here is an example of adding reviewer rights for a specific user to the Inbox in this code if it detects an existing ACE it just removes that ACE and adds a new one with reviewer rights.
- $UsertoAdd = "user@domain.comm"
- $PermissiontoAdd = [Microsoft.Exchange.WebServices.Data.FolderPermissionLevel]::Reviewer
- $existingperm = $null
- foreach($fperm in $Inbox.Permissions){
- if($fperm.UserId.PrimarySmtpAddress -ne $null){
- if($fperm.UserId.PrimarySmtpAddress.ToLower() -eq $UsertoAdd.ToLower()){
- $existingperm = $fperm
- }
- }
- }
- if($existingperm -ne $null){
- $Inbox.Permissions.Remove($existingperm)
- }
- $newfp = new-object Microsoft.Exchange.WebServices.Data.FolderPermission($UsertoAdd,$PermissiontoAdd)
- $Inbox.Permissions.Add($newfp)
- $Inbox.Update()
When working with calendar folder permissions you should also make sure you mirror any changes that you make to the Freebusy folder in the Non_IPM_Subtree if you don't do this you may find that you cant modify appointments even though you have rights in the folder. Eg the following is an example of giving the Default ACE (which essentially means everybody) editor access to a calendar.
- function addFolderPerm($folder){
- $existingperm = $null
- foreach($fperm in $folder.Permissions){
- if($fperm.UserId.StandardUser -eq $null){
- if ($fperm.UserId.PrimarySmtpAddress.ToLower() -eq $NewACLUser.ToLower()){
- $existingperm = $fperm
- }
- }
- }
- if($existingperm -ne $null){
- $folder.Permissions.Remove($existingperm)
- }
- $newfp = new-object Microsoft.Exchange.WebServices.Data.FolderPermission($NewACLUser,$Permission)
- $folder.Permissions.Add($newfp)
- $folder.Update()
- }
- "Checking : " + $MailboxName
- $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
- $Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderidcnt)
- "Set Calendar Rights"
- addFolderPerm($Calendar)
- $sf1 = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"Freebusy Data")
- $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
- $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;
- $folderidRoot = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
- $fiResult = $Service.FindFolders($folderidRoot,$sf1,$fvFolderView)
- if($fiResult.Folders.Count -eq 1){
- "Set FreeBusy Rights"
- $Freebusyfld = $fiResult.Folders[0]
- $Freebusyfld.Load()
- addFolderPerm($Freebusyfld)
- }
Reporting on all Shared folders in a Mailbox
Here's a full script that will enumerate every folder in a mailbox and check all the ACE's and report on any folders that are shared.
- $ReportingCollection = @()
- $MailboxName = "user@domain.com"
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_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
- $creds = New-Object System.Net.NetworkCredential("user@domain.com","password")
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- ## 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
- 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
- }
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- # Bind to the Archive Root folder
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
- $fldRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $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;
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
- #The Search filter will exclude any Search Folders
- $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);
- $psPropertySet.Add($PR_Folder_Path);
- $fvFolderView.PropertySet = $psPropertySet;
- $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
- $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
- $fiResult = $null
- do {
- $fiResult = $Service.FindFolders($folderid,$sfSearchFilter,$fvFolderView)
- foreach($ffFolder in $fiResult.Folders){
- $foldpathval = $null
- 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)
- }
- "Processing : " + $fpath
- $ffFolder.Load()
- foreach($Permission in $ffFolder.Permissions){
- if($Permission.PermissionLevel -ne [Microsoft.Exchange.WebServices.Data.FolderPermissionLevel]::None){
- $rptobj = "" | Select FolderPath, User, Permission
- if($Permission.UserId.StandardUser -eq $null){
- $rptobj.FolderPath = $fpath
- $rptobj.User = $Permission.UserId.PrimarySmtpAddress
- $rptobj.Permission = $Permission.PermissionLevel
- $ReportingCollection += $rptobj
- }
- else{
- $rptobj.FolderPath = $fpath
- $rptobj.User = $Permission.UserId.StandardUser.ToString()
- $rptobj.Permission = $Permission.PermissionLevel
- $ReportingCollection += $rptobj
- }
- }
- }
- }
- $fvFolderView.Offset += $fiResult.Folders.Count
- }while($fiResult.MoreAvailable -eq $true)
- $ReportingCollection
- $ReportingCollection | Export-Csv -NoTypeInformation -Path c:\temp\sharedfolders.csv