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!
Get-MailboxFolderPermission