Remove-MAPIPermissions.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
############################################################################### # Remove all MAPI Permission from a Mailbox from a specific User # V0.1 04.10.2021 - Initial Version - Andres Bohren # V0.2 10.03.2022 - Updates and Cleaning Code - Andres Bohren # V0.3 28.12.2022 - Updates and Cleaning Code - Andres Bohren ############################################################################### ############################################################################## # Remove-MAPIPermission ############################################################################## Function Remove-MAPIPermission { <# .SYNOPSIS Simple way of removing MAPI Permissions from a Mailbox for the default Folders (Inbox, Calendar, Notes, Tasks, Contacts) Also takes care of the 'FolderVisible' Permission in the Root Folder of the Mailbox. .DESCRIPTION Remove MAPI Permission from a specific User Remove-MAPIPermission.ps1 -Mailbox john.doe@yourdomain.ch -Trustee erika.mustermann@yourdomain.com -Folder Inbox -RemoveSendOnBehalf $true .PARAMETER Mailbox The mailbox on which the permission will be removed .PARAMETER Trustee The User which permissions will be removed .PARAMETER Folder A specific defaultfolder: - Inbox - Calendar - Notes - Tasks - Contacts .PARAMETER IncludeSubfolders Boolean Value (True/False) if MAPI Permission are removed on all Subfolders .PARAMETER DeleteRootFolderPermission Boolean Value (True/False) to remove MAPI Permission from Root Folder for Trustee .PARAMETER RemoveSendOnBehalf Boolean Value (True/False) if the User will also be removed from the "SendOnBehalf" Permission Only works if the User is directly assigned. Will not work if he is in Member of an assigned Group. .EXAMPLE Remove-MAPIPermission.ps1 -Mailbox john.doe@yourdomain.ch -User erika.mustermann@yourdomain.com -Folder Inbox Remove-MAPIPermission.ps1 -Mailbox john.doe@yourdomain.ch -User erika.mustermann@yourdomain.com -Folder Calendar [-IncludeSubfolders $true] [-RemoveSendOnBehalf $true] [-DeleteRootFolderPermission $true] #> param( [parameter(Mandatory=$true)][String]$Mailbox, [parameter(Mandatory=$true)][String]$Trustee, [parameter(Mandatory=$true)][String]$Folder, [bool]$includeSubfolders = $false, [bool]$DeleteRootFolderPermission = $false, [bool]$RemoveSendOnBehalf = $false ) #Check Exchange Online Connection $Return = Test-ExchangeConnection If ($Return -eq $false) { Write-host "Not connected to Exchange or Exchange Online. Please connect first to Exchange or Exchange Online." -ForegroundColor Red Break } #Check if Mailbox exists Write-Host "Checking Parameter Mailbox: $Mailbox" $MBX = Get-Mailbox -Identity $Mailbox -ErrorAction SilentlyContinue If ($Null -eq $MBX) { Write-Host "Please Enter a valid Mailbox Emailaddress ($Mailbox)" -ForegroundColor Yellow Write-Host "The script will be ended right now." -ForegroundColor Yellow Break } #Check if Trustee (exists and has the Right Type) Write-Host "Checking Parameter Trustee: $Trustee" $FunctionResult = Get-RecipientType -Emailaddress $Trustee If ($FunctionResult[0] -eq $false) { Write-Host "Trustee Recipient not found. Please Enter a valid Trustee Emailaddress ($Trustee)" -ForegroundColor Yellow $readhost = Read-Host "Maybe it's a deleted Mailbox. Do you want to continue? (y/n)[n]?" if ($readhost -eq "y") { #$TrusteeRecipientType = $FunctionResult[1] $TrusteePrimarySMTPAddress = $Trustee $TrusteeDisplayName = $Trustee $TrusteeAlias = $Trustee } Else { Write-Host "The script will be ended right now." -ForegroundColor Yellow Break } } else { $FunctionResult = Get-RecipientType -Emailaddress $Trustee $TrusteeRecipientType = $FunctionResult[1] $TrusteePrimarySMTPAddress = $FunctionResult[2] $TrusteeDisplayName = $FunctionResult[3] $TrusteeAlias = $FunctionResult[4] } #Check Foldertype Input and Parse to foldername Write-Host "Checking Parameter Folder: $Folder" If ($Folder -ne $null) { $folderstats = Get-MailboxFolderStatistics -Identity $Mailbox | Where-Object {$_.FolderType -ne "CalendarLogging" -AND $_.FolderType -NotLike "Recoverable*" -AND $_.FolderType -NotLike "Yammer*" -AND $_.FolderType -NotLike "BirthdayCalendar"} switch ($Folder) { "Inbox" { $InboxObject = $folderstats | Where-Object FolderType -eq "Inbox" $CustomFolderName = $InboxObject.Name } "Calendar" { $CalendarObject = $folderstats | Where-Object FolderType -eq "Calendar" $CustomFolderName = $CalendarObject.Name } "Notes" { $NotesObject = $folderstats | Where-Object FolderType -eq "Notes" $CustomFolderName = $NotesObject.Name } "Tasks" { $TaskObject = $folderstats | Where-Object FolderType -eq "Tasks" $CustomFolderName = $TaskObject.Name } "Contacts" { $ContactsObject = $folderstats | Where-Object FolderType -eq "Contacts" $CustomFolderName = $ContactsObject.Name } default { Write-Host "The Parameter -Folder is incorrect. Accepted Values are: 'Inbox', 'Calendar', 'Notes', 'Tasks', 'Contacts'" -foregroundColor Yellow Write-Host "The script will be ended right now." -ForegroundColor Yellow Break } } } ########################################################################### #Root Folder Permission ########################################################################### If ($DeleteRootFolderPermission -eq $true) { #Remove View Permission from Root Folder Write-Host "Configure RootFolder Permission" $FolderPermissions = Get-MailboxFolderPermission $Mailbox":\" [bool]$RootPermissionFound = $false Foreach ($Line in $FolderPermissions) { If ($Line.User -eq "Standard" -or $Line.User -eq "Default" -or $Line.User -eq "Anonymous") { If ($Line.AccessRights -eq "FolderVisible") { Write-Host "Default or Anonymous User has 'FolderVisible' Permission" -ForegroundColor yellow } } else { If ($Line.User.Displayname -eq $TrusteeDisplayName) { $RootPermissionFound = $true #AccessRight } } } If ($RootPermissionFound -eq $true) { #Add Prmission Write-Host "REMOVE: "$Mailbox":\ > FolderVisible > $TrusteePrimarySMTPAddress" -ForegroundColor Green Remove-MailboxFolderPermission -Identity $Mailbox":\" -User $TrusteePrimarySMTPAddress -Confirm:$false | out-null } } ########################################################################### #Remove Custom Permission from Folder ########################################################################### Write-Host "Configure Folder <$Folder> Permission" $FolderPermissions = get-MailboxFolderPermission $Mailbox":\$CustomFolderName" #$UserMBXFolderIsNotSet=$true #Check if Trustee is already in the Trustee Array for that Folder Foreach ($Line in $FolderPermissions) { If ($Line.User.Displayname -eq $TrusteeDisplayName) { $AccessRight = $Line.AccessRights Write-Host "REMOVE: "$Mailbox":\$CustomFolderName > $AccessRight > $TrusteePrimarySMTPAddress" -ForegroundColor Green Remove-MailboxFolderPermission -Identity $Mailbox":\$CustomFolderName" -User $Trustee -Confirm:$false | Out-Null #Exit Foreach Break } } ########################################################################### #Subfolders ########################################################################### if ($includeSubfolders -eq $true) { Write-Host "Configure SubFolders of <$Folder>" foreach ($SubFolder in $folderstats) { if ($SubFolder.identity -match "$Mailbox\\$CustomFolderName\\") { $NormalizedSubfolder = $SubFolder.identity.replace($Mailbox,$Mailbox +":") $NormalizedSubfolder = $NormalizedSubfolder -replace([char]63743,"/") $FolderPermissions = get-MailboxFolderPermission $NormalizedSubfolder Foreach ($Line in $FolderPermissions) { If ($Line.User.Displayname -eq $TrusteeDisplayName) { $AccessRight = $Line.AccessRights Write-Host "REMOVE: $NormalizedSubfolder > $AccessRight > $Trustee" -ForegroundColor Green Remove-MailboxFolderPermission -Identity $NormalizedSubfolder -User $TrusteePrimarySMTPAddress -Confirm:$false } } } } } ########################################################################### # SendOnBehalf ########################################################################### If ($RemoveSendOnBehalf -eq $true) { Write-Host "Configure SendOnBehalf" #Get Users in Send On Behalf and Add to Array $SOB = Get-Mailbox $Mailbox | Select-Object GrantSendOnBehalfTo [System.Collections.ArrayList]$SendOnBehalfArr = @() Foreach ($Entry in $SOB.GrantSendOnBehalfTo) { $Recipient = Get-Recipient -Identity "$Entry" | Select-Object DisplayName,Alias,PrimarySmtpAddress $PrimarySmtpAddress = ($Recipient.PrimarySmtpAddress).ToString() If ($PrimarySmtpAddress -ne $TrusteePrimarySMTPAddress) { #Add to Array $SendOnBehalfArr.Add($PrimarySMTPAddress) | Out-Null } } Write-Host "Remove Send on Behalf to $Mailbox for $TrusteePrimarySMTPAddress" -ForegroundColor Green Set-Mailbox $Mailbox -GrantSendOnBehalfTo $SendOnBehalfArr } } |