0

I'm trying to get PowerShell to give permissions to calendars, with the Add-MailboxFolderPermission cmdlet, however, if the permissions are already set, I'd like it to use the Set-MailboxFolderPermission cmdlet.

My script so far looks like this:

$OU = Read-Host 'Type OU Name Here'
$allmailbox = Get-Mailbox -OrganizationalUnit "OU=$OU,OU=Users,DC=Contoso,DC=com"
$User = Read-Host 'Type the username of the user that needs access to the other calendars here. This is typically a manager'
$Rights = Read-Host 'Type the level of access the user should have. Examples include Reviewer, Editor etc'

Foreach ($Mailbox in $allmailbox)

{Add-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}

I'm not sure if I need a Try Catch sort of thing (new to scripting). Maybe something like this?

$OU = Read-Host 'Type OU Name Here'
$allmailbox = Get-Mailbox -OrganizationalUnit "OU=$OU,OU=Users,DC=Contoso,DC=com"
$User = Read-Host 'Type the username of the user that needs access to the other calendars here. This is typically a manager'
$Rights = Read-Host 'Type the level of access the user should have. Examples include Reviewer, Editor etc'

Foreach ($Mailbox in $allmailbox)

Try

{Add-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}

Catch

{Set-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}

Any advice would be appreciated!

2
  • Better check it with Get-MailboxFolderPermission Commented Feb 16, 2016 at 13:34
  • @marsze I know that if I run the first script, and the user hasn't already got any permissions set on it, it will add the permissions, and work as normal. However, if they already have some permissions on it, then Add-MailboxFolderPermission fails, and you need to use Set-MailboxFolderPermission instead. I'd like to build this into the script so that if Add-MailboxFolderPermission fails, it will try Set-MailboxFolderPermission, then move onto the next user, trying Add-MailboxFolderPermission first. Commented Feb 16, 2016 at 13:37

2 Answers 2

1

Probably not a try catch as you could well hide legitimate errors although the set would probably fail in that case too, however could you not use:

$permission = Get-MailboxFolderPermission -identity ($Mailbox.alias+’:\calendar’) -User "<domain>\$user"
if($permission -eq $null){
    Add-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user"  –Accessrights $Rights
}else{
    Set-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights
}
Sign up to request clarification or add additional context in comments.

1 Comment

just to check, the script as a whole would look like this? Foreach ($Mailbox in $allmailbox) $Permission = Get-MailboxFolderPermission -identity ($Mailbox.alias+’:\calendar’) -User "<domain>\$user" if ($Permission -eq $null) {Add-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights} Else {Set-mailboxfolderpermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}
0

@Alexis

Sorry, the formatting came out terrible in the comment.

Just to check, would the script as a whole look like this?

$OU = Read-Host 'Type Bureau Short Code Here'
$allmailbox = Get-Mailbox -OrganizationalUnit "OU=$OU,OU=All Scottish CABs,DC=internal,DC=scottishcabs,DC=org,DC=uk"
$User = Read-Host 'Type the username of the user that needs access to the other calendars here. This is typically a bureau manager'
$Rights = Read-Host 'Type the level of access the user should have. Examples include Reviewer, Editor etc'

Foreach ($Mailbox in $allmailbox){

    $Permission = Get-MailboxFolderPermission -identity ($Mailbox.alias+’:\calendar’) -User "<domain>\$user" 

    if ($Permission -eq $null)

    {Add-MailboxFolderPermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}

    Else

    {Set-mailboxfolderpermission –identity ($Mailbox.alias+’:\calendar’) –user "<domain>\$user" –Accessrights $Rights}
}

1 Comment

looks about right, have added brackets around the code block that should be done foreach. Ahh although apparently it needs to be peered reviewed for others too see. So you need a bracket after foreach($mailbox in $allmailboxes){ and closing at the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.