Exchange – remove and add folder permissions

#IT-Solved
#april 2016
#V3 20160405 automapping uit bij zetten full access
import-module Activedirectory
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
Set-ADServerSettings -ViewEntireForest $true
cls
$all_rights = @()
$mailboxzoek = $null
$mailboxzoek = read-host “Mailboxnaam”

#Mailbox ophalen
if ($($mailboxzoek) -eq “”)
{
Write-Host “Geen mailbox gekozen!” -ForegroundColor Red
exit
}

try {
$mailboxzoek =Get-Mailbox $($mailboxzoek) -ErrorAction Stop
}
Catch
{
Write-Host “Geen geldige mailbox gekozen!” -ForegroundColor Red
exit
}

# Folders ophalen
$mailboxFolders = Get-MailboxFolderStatistics $mailboxzoek | % {$_.folderpath} | % {$_.replace(“/”,”\”)}

#top of information store toevoegen
$mailboxFolders += “\”

#tonen van huidige mailbox
Write-Host “Verwerken van user : “$($mailboxzoek).name -ForegroundColor Yellow

$mb = @{Expression={$($mailboxzoek).Name};Label=”Mailbox”}

#loopen door folder rechten en opniew zetten
ForEach($folder in $mailboxFolders)
{
try
{
$FolderKey = $($mailboxzoek).name + “:” + $folder
$Permissions = Get-MailboxFolderPermission -identity $FolderKey -ErrorAction stop
$mailbox_Calendar_Rights = $Permissions | Where-Object {$_.User -notlike “Default” -and $_.User -notlike “Anonymous” -and $_.AccessRights -notlike “None”} | Select $mb, User, FolderName, @{Expression={$_.AccessRights};Label=”AccessRights”}
$all_rights += $mailbox_Calendar_Rights

if ($mailbox_Calendar_Rights -ne $null)
{
Write-Host “Bezig met folder :”$folder -ForegroundColor Cyan
foreach ($m_right in $mailbox_Calendar_Rights)
{
write-host ” — “$m_right.User /($m_right.User).ADRecipient.Identity.DistinguishedName “($($m_right.AccessRights))” -ForegroundColor green
Remove-MailboxFolderPermission -Identity $FolderKey -User $m_right.User -Confirm:$false
start-sleep -Milliseconds 400
Add-MailboxFolderPermission -Identity $FolderKey -User $m_right.User -AccessRights $m_right.AccessRights
}
}
}
catch
{
write-host “Map niet toegankelijk Set ($folder)” -ForegroundColor Red
}
}

Geplaatst in Exchange, Powershell.