excal-arights/run-script.ps1

78 lines
3.0 KiB
PowerShell

<##
# EXCAL-ARIGHTS - runable script (entrypoint)
#
# @version 1.0.0
# @since 08-25-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
$CountOK = 0
$CountUpdated = 0
$CountError = 0
# Initiate exchange connection
Connect-ExchangeOnline -UserPrincipalName $AdminAccount -ShowProgress $true
# 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?") -eq $false) {
Stop-Transcript
Exit
}
# Retrieving users mailbox
$Users = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Where-Object {$_.Alias -notin $IgnoreAlias}
Write-Host ("[$ScriptName] We found " + $Users.count + " users`n") -BackgroundColor White -ForegroundColor Black
# Processing users mailbox list
foreach ($User in $Users) {
Write-Host $user.Name -BackgroundColor DarkBlue
try {
$Calandars = Get-MailboxFolderStatistics $User.Identity -FolderScope Calendar | Where-Object {$_.Name -in $CalandarFolders}
# Process user calandars
foreach ($Calandar in $Calandars) {
$CurrCalId = $User.Identity.ToString() + ":\" + $Calandar.Name.ToString()
$CurrCalPerm = Get-MailboxFolderPermission -Identity $CurrCalId -User Default
# Check and update permission
if ($PermissionsTrigger -Contains $CurrCalPerm.AccessRights) {
Write-Host " [X] " -NoNewline
Write-Host ("Updated : The permission has been updated from (" + $CurrCalPerm.AccessRights + ") to (" + $Permission + ").") -ForegroundColor Green
$CountOK++
# Update permission (for debugging: -WhatIf)
Set-MailboxFolderPermission -Identity $CurrCalId -User Default -AccessRights $Permission
} else {
Write-Host " [X] " -NoNewline
Write-Host ("Ignored : The permission (" + $CurrCalPerm.AccessRights + ") of this user did not launch the trigger.") -ForegroundColor Yellow
$CountUpdated++
}
}
}
catch {
Write-Host "[ERROR] An unexpected error occured." -ForegroundColor DarkRed
Write-Host "--> $_" -ForegroundColor DarkRed
$CountError++
}
Write-Host
}
# Showing resultats
DialogSay "$ScriptName" "The update was performed successfuly.`n`n$CountOK account(s) was already correctly configured.`n$CountUpdated account(s) were updated.`n$CountError error(s) occurred."
# Stop logging
Stop-Transcript