diff --git a/configs.ps1 b/configs.ps1 index e717e5e..a9a7791 100644 --- a/configs.ps1 +++ b/configs.ps1 @@ -27,6 +27,9 @@ $CalandarFolders = @("Agenda", "Calendar", "Calendrier", "Kalender") # Alias of account to be skipped (an alias, is the email prefix ahead of the @domain.com) $IgnoreAlias = @("raoul.nihart", "brecht.marsoul") +# Name of account to be skipped when permission for it's is set into user's calandar (a full name ex: Jeremi Nihart) +$IgnoreUsers = @("Anonymous") + # Email of the Administration account to use $AdminAccount = "admin@luminussolutions.be" diff --git a/run-script.ps1 b/run-script.ps1 index d8037d7..4e3dd17 100644 --- a/run-script.ps1 +++ b/run-script.ps1 @@ -12,7 +12,7 @@ # Setup script $Root = (Split-Path $MyInvocation.MyCommand.Path -Parent) -$time = (Get-Date -Format "MM-dd-yyyy-HH-mm-ss") +$Time = (Get-Date -Format "MM-dd-yyyy-HH-mm-ss") . $Root\configs.ps1 . $Root\utils\functions.ps1 Import-Module ExchangeOnlineManagement @@ -22,13 +22,18 @@ Add-Type -AssemblyName PresentationFramework Start-Transcript -Path $Root\$LogFile -Append # Statistics variables -$CountOK = 0 -$CountUpdated = 0 -$CountError = 0 +$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 ?") -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?") -eq $false) { Stop-Transcript @@ -36,43 +41,51 @@ if ((DialogAsk "$ScriptName" "Do you want to continue and change the permission } # 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 +$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 + Write-Host $User.Name -BackgroundColor DarkBlue try { - $Calandars = Get-MailboxFolderStatistics $User.Identity -FolderScope Calendar | Where-Object {$_.Name -in $CalandarFolders} + $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() - $CurrCalPerm = Get-MailboxFolderPermission -Identity $CurrCalId -User Default + $CurrCalPerms = Get-MailboxFolderPermission -Identity $CurrCalId | Where-Object {$_.Name -notin $IgnoreUsers -and $_.AccessRights -in $PermissionsTrigger} + $CountLocalUpdates = 0 - # 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++ + # 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 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++ + Set-MailboxFolderPermission -Identity $CurrCalId -User $CurrCalPerm.User.ToString() -AccessRights $Permission -WhatIf + $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 - $CountError++ + $CountErrors++ } 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." +DialogSay "$ScriptName" "The update was performed successfuly.`n`n$CountUpdates has been updated on $CountUsers`n$CountErrors errors occured." # Stop logging Stop-Transcript \ No newline at end of file diff --git a/utils/dev-note.txt b/utils/dev-note.txt index f573b46..27e9136 100644 --- a/utils/dev-note.txt +++ b/utils/dev-note.txt @@ -1,4 +1,6 @@ Get-ExecutionPolicy Set-ExecutionPolicy Unrestricted -Set-ExecutionPolicy Restricted \ No newline at end of file +Set-ExecutionPolicy Restricted + +un objet | Get-Member -MemberType Property \ No newline at end of file diff --git a/utils/functions.ps1 b/utils/functions.ps1 index d2f93db..b82207a 100644 --- a/utils/functions.ps1 +++ b/utils/functions.ps1 @@ -27,4 +27,27 @@ Function DialogSay { [string]$Message ) [System.Windows.MessageBox]::Show($Message, $Title, 'OK', 'Information') +} + +# Show current permissions for all calandars and users +Function DisplayAll { + param ( + [string[]]$CalandarFolders + ) + $Users = Get-EXOMailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox + Write-Host ("[$ScriptName] We found " + $Users.count + " users") -BackgroundColor White -ForegroundColor Black + foreach ($User in $Users) { + Write-Host $User.Name -BackgroundColor DarkGreen + $Calandars = Get-EXOMailboxFolderStatistics $User.Identity -FolderScope Calendar | Where-Object {$_.Name -in $CalandarFolders} + foreach ($Calandar in $Calandars) { + Write-Host $Calandar.Name.ToString() -BackgroundColor DarkMagenta + $CurrCalId = $User.Identity.ToString() + ":\" + $Calandar.Name.ToString() + $CurrCalPerms = Get-MailboxFolderPermission -Identity $CurrCalId + + foreach ($CurrCalPerm in $CurrCalPerms) { + Write-Host ($CurrCalPerm.User.ToString() +" : " + $CurrCalPerm.AccessRights) + } + } + Write-Host + } } \ No newline at end of file