2022-08-25 17:27:45 +02:00
<# #
# EXCAL-ARIGHTS - runable script (entrypoint)
#
2022-08-26 14:10:42 +02:00
# @version 1.1.1
2022-08-26 09:48:05 +02:00
# @since 08-26-2022
2022-08-25 17:27:45 +02:00
#
# @author Jérémi Nihart <contact@endmove.eu>
# @copyright © 2022 EndMove, All rights reserved.
#
# @link https://git.endmove.eu/EndMove/excal-arights
#>
# Setup script
$Root = ( Split-Path $MyInvocation . MyCommand . Path -Parent )
2022-08-25 23:26:12 +02:00
$Time = ( Get-Date -Format " MM-dd-yyyy-HH-mm-ss " )
2022-08-25 17:27:45 +02:00
. $Root \ configs . ps1
. $Root \ utils \ functions . ps1
Import-Module ExchangeOnlineManagement
Add-Type -AssemblyName PresentationFramework
# Start logging
Start-Transcript -Path $Root \ $LogFile -Append
# Statistics variables
2022-08-25 23:26:12 +02:00
$CountUsers = 0
$CountUpdates = 0
$CountErrors = 0
2022-08-25 17:27:45 +02:00
# Initiate exchange connection
Connect-ExchangeOnline -UserPrincipalName $AdminAccount -ShowProgress $true
2022-08-25 23:26:12 +02:00
# Do you want to preview current permissions ?
2022-08-26 09:48:05 +02:00
if ( ( DialogAsk " $ScriptName " " Do you want to preview all calandar permissions ? `n This action may take more than 5 minutes. " ) -eq $true ) {
2022-08-25 23:26:12 +02:00
DisplayAll $CalandarFolders
}
2022-08-25 17:27:45 +02:00
# Do you realy want to change permissions ?
2022-08-26 09:48:05 +02:00
if ( ( DialogAsk " $ScriptName " " Do you want to continue and change the permission of the calendars according to your current configuration? `n If no script will be aborted. " ) -eq $false ) {
2022-08-25 17:27:45 +02:00
Stop-Transcript
Exit
}
# Retrieving users mailbox
2022-08-25 23:26:12 +02:00
$Users = Get-EXOMailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Where-Object { $_ . Alias -notin $IgnoreAlias }
$CountUsers = $Users . count
2022-08-26 14:10:42 +02:00
Write-Host ( " [ $ScriptName ] We found $CountUsers user mailboxes " ) -BackgroundColor White -ForegroundColor Black
2022-08-25 17:27:45 +02:00
# Processing users mailbox list
2022-08-25 23:26:12 +02:00
# Info: all these loops are here because we want to follow up as we
# process the data (too much symplification is not always a good thing).
2022-08-25 17:27:45 +02:00
foreach ( $User in $Users ) {
2022-08-25 23:26:12 +02:00
Write-Host $User . Name -BackgroundColor DarkBlue
2022-08-25 17:27:45 +02:00
try {
2022-08-25 23:26:12 +02:00
$Calandars = Get-EXOMailboxFolderStatistics $User . Identity -FolderScope Calendar | Where-Object { $_ . Name -in $CalandarFolders }
2022-08-25 17:27:45 +02:00
# Process user calandars
foreach ( $Calandar in $Calandars ) {
2022-08-25 23:26:12 +02:00
Write-Host $Calandar . Name . ToString ( ) -BackgroundColor DarkMagenta
2022-08-25 17:27:45 +02:00
$CurrCalId = $User . Identity . ToString ( ) + " :\ " + $Calandar . Name . ToString ( )
2022-08-26 14:10:42 +02:00
$CurrCalPerms = Get-EXOMailboxFolderPermission -Identity $CurrCalId | Where-Object { $_ . Name -notin $IgnoreUsers -and $_ . AccessRights -in $PermissionsTrigger }
2022-08-25 23:26:12 +02:00
$CountLocalUpdates = 0
# Nothing to change ?
if ( $CurrCalPerms . count -eq 0 ) {
Write-Host ( " OK --> The permissions are in accordance with the current configuration. " ) -ForegroundColor Yellow
}
2022-08-25 17:27:45 +02:00
2022-08-25 23:26:12 +02:00
# Process caladar permissions
foreach ( $CurrCalPerm in $CurrCalPerms ) {
Write-Host ( " UPDATE --> Permission for " + $CurrCalPerm . User . ToString ( ) + " has been switch from " + $CurrCalPerm . AccessRights + " to " + $Permission + " . " ) -ForegroundColor Green
2022-08-25 18:44:16 +02:00
# Update permission (for debugging: -WhatIf)
2022-08-26 09:48:05 +02:00
Set-MailboxFolderPermission -Identity $CurrCalId -User $CurrCalPerm . User . ToString ( ) -AccessRights $Permission
2022-08-25 23:26:12 +02:00
$CountLocalUpdates + +
2022-08-25 18:17:55 +02:00
}
2022-08-25 17:27:45 +02:00
}
2022-08-25 23:26:12 +02:00
Write-Host ( " ( $CountLocalUpdates permissions updated for $CurrCalId ) " ) -BackgroundColor White -ForegroundColor Black
$CountUpdates + = $CountLocalUpdates
2022-08-25 17:27:45 +02:00
}
catch {
Write-Host " [ERROR] An unexpected error occured. " -ForegroundColor DarkRed
Write-Host " --> $_ " -ForegroundColor DarkRed
2022-08-25 23:26:12 +02:00
$CountErrors + +
2022-08-25 17:27:45 +02:00
}
Write-Host
}
# Showing resultats
2022-08-25 23:26:12 +02:00
DialogSay " $ScriptName " " The update was performed successfuly. `n `n $CountUpdates has been updated on $CountUsers `n $CountErrors errors occured. "
2022-08-25 17:27:45 +02:00
2022-08-26 09:48:05 +02:00
# Do you want to view the new permissions ?
if ( ( DialogAsk " $ScriptName " " Do you want to view all new permission ? `n This action may take more than 5 minutes. " ) -eq $true ) {
DisplayAll $CalandarFolders
}
Read-Host " The script has finished, press ENTER to continue... "
2022-08-25 17:27:45 +02:00
# Stop logging
Stop-Transcript