Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

add access for a user to other mailbox, but check first if the user already is granted access

$
0
0

Hi

I have a script that traverses all mailboxes and give a specified user [User1] access to all. I run the script whenever new users are added.

But I want the script to check if [User1] already is granted access and skip the command and go to next mailbox. Then I would get an output only for new mailboxes that [User1] is granted access.

Script:
foreach ($mailbox in $mailboxes) {
#Retrieve name of the user`s calendar
$calendar = (($mailbox.SamAccountName)+ ":\" + (Get-MailboxFolderStatistics -Identity $mailbox.SamAccountName -FolderScope Calendar | Select-Object -First 1).Name)
#Check if calendar-permission for user [User1] is given Access: "PublishingEditor"
    if (((Get-MailboxFolderPermission $calendar  | Where-Object {$_.User -eq "[User1]"}).AccessRights) -notlike "PublishingEditor" ) {

    Write-Host "Adding calendar permission "PublishingEditor" for [User1] to mailbox: $mailbox..." -ForegroundColor Yellow
    Add-MailboxFolderPermission -User "[User1]" -AccessRights "PublishingEditor" -Identity $calendar 
    }
}

This script doesn't work the way I want, but it taverses all mailboxes and running the Add-mailbox...-command for all mailboxes wheter [User1] is granted access or not. I receive an error for all mailboxes that User1 already is granted access:

An existing permission entry was found for user: [User1].

How can I modify the IF-statement so the script skips mailboxes that [User1] already has access to?


Regards Terje


Viewing all articles
Browse latest Browse all 10624

Trending Articles