Set Calendar Permissions v2
Wednesday 13 April 2011 - Filed under Exchange + Powershell
I was recently requested to add “Reviewer” rights for a specific user on every calendar hosted on my Exchange 2010 SP1 environment. Looking to automate the process I started looking around for a way to complete this task via PowerShell.
I came across a script written by Jan Egil Ring that adds “Reviewer” permissions for the “Default” user on all mailboxes queried. Using this script as a guideline I wrote a dynamic script that would allow me to set any permission for any user to all mailboxes by simply passing specific variables to the script.
Example Run:
(Give CalendarView group ‘Owner’ rights on all calendars)
“c:\scripts\Set-CalendarPermissions-v2.ps1 CalendarView Owner”
I run this script daily as a scheduled task so I added some Write-Output commands to allow the on-screen Write-Host outputs to be piped to a text file by using the >c:\output.txt after the command.
param([string]$AccessUser,[string]$AccessRights)
#NAME: Set-CalendarPermissions-v2.ps1
#ORIGINAL AUTHOR: Jan Egil Ring
#ORIGINAL COMMENT: Script to set calendar-permission for mailboxes in Exchange Server 2010. #More information: http://blog.powershell.no/2010/09/20/managing-calendar-permissions-in-exchange-server-2010
#COMMENT: Run as .\Set-CalendarPermissions-v2.ps1 [USERNAME/GROUP] [PERMISSION]
#You must set a username, however if you leave out the permission parameter the script will default to 'Reviewer'
#LAST UPDATED: 4/13/2011 10:56PM
Write-Host "Starting Script Set-CalendarPermissions.ps1"
Write-Output "Starting Script Set-CalendarPermissions.ps1"
IF (!$AccessUser){
Write-Host ""
Write-Output ""
Write-Host "AccessUser Parameter has not been set..." -ForegroundColor Red
Write-Output "AccessUser Parameter has not been set..."
Write-Host "Run as '.\Set-CalendarPermissions-v2.ps1 CalendarView" -ForegroundColor White
Write-Output "Run as '.\Set-CalendarPermissions-v2.ps1 CalendarView"
Write-Host ""
exit
}
Write-Host "AccessRight Set to $AccessUser"
Write-Output "AccessRight Set to $AccessUser"
IF (!$AccessRights){
$AccessRights = "Reviewer"
}
Write-Host "AccessRight Set to $AccessRights"
Write-Output "AccessRight Set to $AccessRights"
#requires -version 2
#Load Exchange Server 2010 Management Shell if not loaded. You may delete/comment out this step if you are running the script from the Exchange Management Shell
if (-not (Get-PSSnapin | Where-Object {$_.Name -like "Microsoft.Exchange.Management.PowerShell.E2010"})){
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
#Custom variables
$mailboxes = Get-Mailbox
#Loop through all mailboxes
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 "AccessUser"
if (-not ((Get-MailboxFolderPermission $calendar | Where-Object {$_.User -like $AccessUser}))) {
Write-Host "Updating calendar permission for $mailbox..." -ForegroundColor Yellow
Write-Output "Updating calendar permission for $mailbox..."
Add-MailboxFolderPermission -User $AccessUser -AccessRights $AccessRights -Identity $calendar
}
#Check if calendar-permission for user "AccessUser" is set to the default permission of "Reviewer"
if (((Get-MailboxFolderPermission $calendar | Where-Object {$_.User -like $AccessUser}).AccessRights) -notlike "Reviewer" ) {
Write-Host "Updating calendar permission for $mailbox..." -ForegroundColor Yellow
Write-Output "Updating calendar permission for $mailbox..."
#Set calendar-permission for user "Default" to value defined in variable $AccessRights
Set-MailboxFolderPermission -User $AccessUser -AccessRights $AccessRights -Identity $calendar
}
}
You can download a copy of this script here: Set-CalendarPermissions-v2.ps1
1 comment :: Share or discuss :: 2011-04-13 :: Kevin
(144)
(10)
(0)