<#
.SYNOPSIS
MailboxFolderPermissions.ps1

.DESCRIPTION 
A proof of concept script for adding mailbox folder
permissions to all folders in a mailbox.

.OUTPUTS
Console output for progress.

.PARAMETER Mailbox
The mailbox that the folder permissions will be added to.

.PARAMETER User
The user you are granting mailbox folder permissions to.

.PARAMETER Access
The permissions to grant for each folder.

.EXAMPLE
.\MailboxFolderPermissions.ps1 -MailboxToAddPermission onstestuser -UserToGivePermission igbina -AccessType Reviewer

This will grant igbina "Reviewer" access to all folders in onstestuser's mailbox.
Other PermissionTypes:
•None   FolderVisible
 
•Owner   CreateItems, ReadItems, CreateSubfolders, FolderOwner, FolderContact, FolderVisible, EditOwnedItems, EditAllItems, DeleteOwnedItems, DeleteAllItems
 
•PublishingEditor   CreateItems, ReadItems, CreateSubfolders, FolderVisible, EditOwnedItems, EditAllItems, DeleteOwnedItems, DeleteAllItems
 
•Editor   CreateItems, ReadItems, FolderVisible, EditOwnedItems, EditAllItems, DeleteOwnedItems, DeleteAllItems
 
•PublishingAuthor   CreateItems, ReadItems, CreateSubfolders, FolderVisible, EditOwnedItems, DeleteOwnedItems
 
•Author   CreateItems, ReadItems, FolderVisible, EditOwnedItems, DeleteOwnedItems
 
•NonEditingAuthor   CreateItems, ReadItems, FolderVisible
 
•Reviewer   ReadItems, FolderVisible
 
•Contributor   CreateItems, FolderVisible

.LINK
http://exchangeserverpro.com/grant-read-access-exchange-mailbox/

.NOTES
Written by: Alberto Igbinedion


Change Log:
V1.00, 15/10/2014 - Initial version
#>

#requires -version 2

[CmdletBinding()]
param (
	[Parameter( Mandatory=$true)]
	[string]$MailboxToAddPermission,
    
	[Parameter( Mandatory=$true)]
	[string]$UserToGivePermission,
    
  	[Parameter( Mandatory=$true)]
	[string]$AccessType
)


#...................................
# Variables
#...................................

$exclusions = @("/Sync Issues",
                "/Sync Issues/Conflicts",
                "/Sync Issues/Local Failures",
                "/Sync Issues/Server Failures",
                "/Recoverable Items",
                "/Deletions",
                "/Purges",
                "/Versions",
                "/Calendar Logging"
                )


#...................................
# Initialize
#...................................

<#

#Add Exchange 2010 snapin if not already loaded in the PowerShell session
if (!(Get-PSSnapin | where {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"}))
{
	try
	{
		Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP
	}
	catch
	{
		#Snapin was not loaded
		Write-Warning $_.Exception.Message
		EXIT
	}
	. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
	Connect-ExchangeServer -auto -AllowClobber
}


#>

#Set scope to include entire forest
if (!(Get-ADServerSettings).ViewEntireForest)
{
	Set-ADServerSettings -ViewEntireForest $true -WarningAction SilentlyContinue
}


#...................................
# Script
#...................................

$mailboxfolders = @(Get-MailboxFolderStatistics $MailboxToAddPermission | Where {!($exclusions -icontains $_.FolderPath)} | Select FolderPath)

foreach ($mailboxfolder in $mailboxfolders)
{
    $folder = $mailboxfolder.FolderPath.Replace("/","\")
    if ($folder -match "Top of Information Store")
    {
       $folder = $folder.Replace(“\Top of Information Store”,”\”)
    }
    $identity = "$($MailboxToAddPermission):$folder"
    Get-MailboxFolderPermission -Identity $identity -User $UserToGivePermission -ErrorAction silentlycontinue
    Write-Host "Adding $UserToGivePermission to $identity with $AccessType permissions"
    try
    {
        Add-MailboxFolderPermission -Identity $identity -User $UserToGivePermission -AccessRights $AccessType -ErrorAction STOP
    }
    catch

    {
       
       Set-MailboxFolderPermission -Identity $identity -User $UserToGivePermission -AccessRights $AccessType
       
       }

       Get-MailboxFolderPermission -Identity $identity -User $UserToGivePermission -ErrorAction silentlycontinue
     
     }
  
#}
##.........Set Calander Permission For Single User
#   Add-MailboxFolderPermission richard.heys@exchange.ons.gov.uk:\calendar -User Hollie.nash –AccessRights owner

# Remove Permission
<#
Get-MailboxPermission -Identity "david.best@ons.gov.uk" | ft user,accessrights Remove-MailboxPermission david.best@ons.gov.uk -User laura.kirke@ons.gov.uk -AccessRights FullAccess
#>
#...................................
# End
#...................................

# Good To Go