Powershell – Set Calendar Permissions on Office365 for list of users given in CSV File

|
| TechPress-KBID-171
Home » Powershell » Powershell – Set Calendar Permissions on Office365 for list of users given in CSV File

Sharing a script to Set/Apply the Calendar permissions on list of users as mentioned in the CSV File. Please see the format of the CSV file. I created and utilized the script when i migrated Exchange 2010 users to Office 365. Exported the Calendar Permissions of all the users from Exchange 2010 and then saved it in a csv file (as you can see in below screenshot) and Applied the permissions using this script. Please see the screenshots and use the help information in the script to know more details.

The Script will try to establish connection with Office365 first. Please provide appropriate username and password to provide relevant permissions to execute this script. Please also keep 2-3 users first in UserList.csv file first and before running it on all the users. This is a best practice i follow to make sure the script will run successfully on all the users.

UserList.csv File Format (Please make sure to keep the same headers (Mailbox,User, AccessRights) as in the Below file:

Example I

Dot Source the Script 

PS> . D:\Scripts\Set-CalendarPermission.ps1

PS> Get-Help Set-CalendarPermissions

This image has an empty alt attribute; its file name is image-1.png

Example II (Execution of the Script)

POWERSHELL SCRIPT:

Download the Script from Microsoft TechNet Gallery. Set-CalendarPermission

Function Set-CalendarPermission { 
<# 
.SYNOPSIS 
Apply/SET the Calendar Permissions on list of mailboxes and Permissions mentioned in the CSV File. 
 
.DESCRIPTION 
This is Set-CalendarPermission Function which can be used to set the Calendar Permissions. 
 
.NOTES     
        Name       : Set-CalendarPermission  
        Author     : Jatin Makhija  
        Version    : 1.0.0  
        DateCreated: 30-Nov-2019  
        Blog       : https://www.techpress.net
         
.LINK 
https://www.techpress.net
 
.EXAMPLE 
PS> Set-CalendarPermission <CSV File Path> 
 
For Example 
 
Set-CalendarPermission C:\Temp\UserList.csv 
 
.EXAMPLE 
 
Set-CalendarPermission 
 
#>  [cmdletBinding()] 
    param( 
    [parameter(mandatory = $true, Position =0, HelpMessage="Please Enter the Path to CSV File")] 
    [string]${Please Enter CSV File Path} 
         ) 
 
Write-Host "Connecting to Office365" -ForegroundColor Blue -BackgroundColor White 
Write-Host "Please Provide your Office365 Global Administrator UserName and Password or the User having appropriate rights on EXO to Update Users Calendar Permissions" -ForegroundColor Blue -BackgroundColor White 
 
Try { 
 
    $ExCred = Get-Credential  
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $ExCred -Authentication Basic -AllowRedirection 
    Import-PSSession $Session 
    Write-Host "Connection to Office365 Established" -ForegroundColor Blue -BackgroundColor White 
 
    } 
 
Catch { 
 
Write-Warning "User not Authenticated / User Credentials not Accepted / User Credentials not Provided" 
Write-Warning $_ 
break 
 
} 
 
 
Import-Csv ${Please Enter CSV File Path} -Header Mailbox, User, AccessRights | ForEach-Object{ 
                                                $MailboxName = $_.Mailbox 
                                                $UserName = $_.User 
                                                $AccessRights = $_.AccessRights 
 
If ($(Get-Aduser -filter {DisplayName -eq $MailboxName}).enabled -eq $false) 
{ 
    Write-Host "$MailboxName" is a Disabled User Account in AD -ForegroundColor DarkYellow  
} 
 
Else 
{ 
    if (Get-Mailbox $MailboxName -ErrorAction SilentlyContinue) 
    { 
            
             Try{ 
                       
                        $userList = Get-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" | Select -ExpandProperty User | Select -Expandproperty DisplayName 
                        if ($userName -in $Userlist) 
                        { 
                           $userrights = Get-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$userName" | Select -ExpandProperty AccessRights 
                            if ($userrights -eq $AccessRights) 
                             {  
                                 Write-Host User "$UserName" is having "$userrights" on "$MailboxName" Already NO ACTION REQUIRED. -ForegroundColor Green 
                             } 
                            
                            Else 
                             { 
                                 Write-Host User "$UserName" already having permission assigned. Existing Permission "$Userrights" **** New Permission: "$accessrights" 
                                 Write-Host SETTING Permissions as per the CSV File to "$AccessRights" 
                                 Set-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$UserName" -AccessRights $AccessRights 
                                 Write-Host "Permission "$AccessRights" on User "$UserName" SET now." -ForegroundColor Black -BackgroundColor White 
                             } 
                        } 
                       
 
                    Else 
                    {                                
                        Write-Host User "$Username" is not having Permission on $MailboxName. Therefore will use the ADD Permission Now 
                        Write-Host "Processing ADD command for User $MailboxName"  
 
                        If (Get-Mailbox $UserName -ErrorAction SilentlyContinue) 
                        { 
                            Add-MailboxFolderPermission -Identity "$($MailboxName):\Calendar" -User "$UserName" -AccessRights $AccessRights 
                        } 
 
                        Else { Write-Host ""$UserName" Mailbox does not exist - Can't ADD the Permission" -ForegroundColor Gray} 
                    } 
                 } 
 
              
                      Catch 
                        { 
                            Write-Warning $_ 
                        }    
 
               } 
 
             Else 
             { 
                Write-Host "$MailboxName" Mailbox does not exist -ForegroundColor Gray 
             } 
 
      
        
} 
} 
 
Write-Host "Removing PSSession $Session" -ForegroundColor Blue -BackgroundColor White 
 
Remove-PSSession $Session 
}

Leave a Reply