workWithPublicFolderPermissions.ps1
#WorkwithPublicFolderPermission.ps1 / js /2021
# addiere oder entferne einen Benutzer an einem PublicFolder
# erforderliche Resourcenoptionen:
# $username # wird immer von Tenfold mitgeliefert
# $cred # konfigurierte Zugangsdaten für den Exchangeserver
# $connectionUri # z.B. "https://myexchangesrv/PowerShell"
# $pfname # einfacher Name des öffentlichen Ordners z.B. "\ABTEILUNG1"
# $pfrights # zu konfigurierendes Benutzerrecht z.B. "PublishingEditor"
# erforderliche Resourcenbedingungen:
# "Resource - New"
# "Resource - Delete"
##############################################################
$username = $params.request.person.masterdata.userName
$connectionUri = $params.connectionUri
$pfrights = $params.pfrights
$pfname = $params.pfname
 
if(!($session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri -Authentication Basic -Credential $cred))
 
{
    return "error: Failed to create remote PowerShell session to "
    exit
}
$progresspreference='SilentlyContinue'
Import-PSSession $session -AllowClobber -DisableNameChecking 
Import-module ActiveDirectory -DisableNameChecking 
 
$displayname = Get-ADUser $username | select -expand Name
 
$status = Get-PublicFolder $pfname -recurse | Get-PublicFolderClientPermission | where {$_.user -like $displayname}
 
if($status)
	{
       #    echo "$username have already access --> remove from Publicfolder $pfname"
    try   {
            Get-PublicFolder $pfname -recurse | Remove-PublicFolderClientPermission -User $displayname -confirm:$false
          }
    catch {
            Remove-PSSession $session
            return "error: failed to remove permissions of $username from publicfolder $pfname";
            exit;
          }
	}
else
	{  
        #   echo "$username have no access --> add to Publicfolder $pfname"
    try   {
            Get-PublicFolder $pfname | Add-PublicFolderClientPermission -User $displayname -AccessRights $pfrights -confirm:$false
            Get-PublicFolder $pfname -recurse | Set-PublicFolderClientPermission -User $displayname -AccessRights $pfrights -confirm:$false
          }
    catch {
            Remove-PSSession $session
            return "error: failed to remove permissions of $username from mailbox $mailbox";
            exit;
          }
	}
 
Remove-PSSession $session
##############################################################