Set calendar permissions for multiple users based on current permissions
Emily Wong
I need some help with my script that I've come up with that I'm having some trouble getting it to work without giving an error or warning message.
So far I have this logic that gives "reviewer" permission to everyone's calendar individually to a specific mail-enabled AD security group:
$users = Get-Mailbox | Select -ExpandProperty Alias;
Foreach ($user in $users) {Add-MailboxFolderPermission $user":\Calendar" -user *GROUP1* -accessrights Reviewer};This works fine, but we create and remove user accounts often so I need the script to run as economically as possible. This logic above will check and error on each user that already has a permission set, which can take a while and adds a lot to the error logs.
What I would like is an IF statement, or revision to the Select property in the script that can check if a user has currently no permissions set to the security group, then add the permission, so if they have the permission set already it will just skip.
Something like this:
$users = Get-Mailbox | Select -ExpandProperty Alias;
$users2 = Get-MailboxFolderPermission -Identity $users:\Calendar -User *GROUP1* IF accessrights = NULL** ???;
Foreach ($user in **$users2**) {Add-MailboxFolderPermission $user":\Calendar" -user *GROUP1* -accessrights Reviewer}; 2 1 Answer
Here's a way to use some conditional PowerShell logic with some arrays, loops, and variables to only run the Add-MailboxFolderPermission command if the "Group Name" does not already have access. I've also included some supporting resources for further reading on the components of the logic.
$gName = "GROUP1";
$accounts = (Get-Mailbox | Select -ExpandProperty Alias);
$accounts | % { $a = $_; $z = (Get-MailboxFolderPermission -Identity $a":\Calendar"); $y = ($z | %{$_.User.DisplayName}); If($y -notcontains $gName){Add-MailboxFolderPermission $a":\Calendar" -user $gName -accessrights Reviewer}; };Supporting Resources
- ForEach-Object
Standard Aliases for Foreach-Object: the '
%' symbol, ForEach - If
- Comparison Operators
- Arrays