excal-arights/run-script.ps1

98 lines
3.9 KiB
PowerShell

<##
# EXCAL-ARIGHTS - runable script (entrypoint)
#
# @version 1.1.0
# @since 08-26-2022
#
# @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)
$Time = (Get-Date -Format "MM-dd-yyyy-HH-mm-ss")
. $Root\configs.ps1
. $Root\utils\functions.ps1
Import-Module ExchangeOnlineManagement
Add-Type -AssemblyName PresentationFramework
# Start logging
Start-Transcript -Path $Root\$LogFile -Append
# Statistics variables
$CountUsers = 0
$CountUpdates = 0
$CountErrors = 0
# Initiate exchange connection
Connect-ExchangeOnline -UserPrincipalName $AdminAccount -ShowProgress $true
# Do you want to preview current permissions ?
if ((DialogAsk "$ScriptName" "Do you want to preview all calandar permissions ?`nThis action may take more than 5 minutes.") -eq $true) {
DisplayAll $CalandarFolders
}
# Do you realy want to change permissions ?
if ((DialogAsk "$ScriptName" "Do you want to continue and change the permission of the calendars according to your current configuration?`nIf no script will be aborted.") -eq $false) {
Stop-Transcript
Exit
}
# Retrieving users mailbox
$Users = Get-EXOMailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Where-Object {$_.Alias -notin $IgnoreAlias}
$CountUsers = $Users.count
Write-Host ("[$ScriptName] We found $CountUsers users") -BackgroundColor White -ForegroundColor Black
# Processing users mailbox list
# 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).
foreach ($User in $Users) {
Write-Host $User.Name -BackgroundColor DarkBlue
try {
$Calandars = Get-EXOMailboxFolderStatistics $User.Identity -FolderScope Calendar | Where-Object {$_.Name -in $CalandarFolders}
# Process user calandars
foreach ($Calandar in $Calandars) {
Write-Host $Calandar.Name.ToString() -BackgroundColor DarkMagenta
$CurrCalId = $User.Identity.ToString() + ":\" + $Calandar.Name.ToString()
$CurrCalPerms = Get-MailboxFolderPermission -Identity $CurrCalId | Where-Object {$_.Name -notin $IgnoreUsers -and $_.AccessRights -in $PermissionsTrigger}
$CountLocalUpdates = 0
# Nothing to change ?
if ($CurrCalPerms.count -eq 0) {
Write-Host (" OK --> The permissions are in accordance with the current configuration.") -ForegroundColor Yellow
}
# 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
# Update permission (for debugging: -WhatIf)
Set-MailboxFolderPermission -Identity $CurrCalId -User $CurrCalPerm.User.ToString() -AccessRights $Permission
$CountLocalUpdates++
}
}
Write-Host ("($CountLocalUpdates permissions updated for $CurrCalId)") -BackgroundColor White -ForegroundColor Black
$CountUpdates += $CountLocalUpdates
}
catch {
Write-Host "[ERROR] An unexpected error occured." -ForegroundColor DarkRed
Write-Host "--> $_" -ForegroundColor DarkRed
$CountErrors++
}
Write-Host
}
# Showing resultats
DialogSay "$ScriptName" "The update was performed successfuly.`n`n$CountUpdates has been updated on $CountUsers`n$CountErrors errors occured."
# Do you want to view the new permissions ?
if ((DialogAsk "$ScriptName" "Do you want to view all new permission ?`nThis action may take more than 5 minutes.") -eq $true) {
DisplayAll $CalandarFolders
}
Read-Host "The script has finished, press ENTER to continue..."
# Stop logging
Stop-Transcript