From 41f0b860735eb6b09bd4b7751290cfc40e1bc887 Mon Sep 17 00:00:00 2001 From: Georgi Matev Date: Wed, 15 Mar 2023 12:07:39 -0400 Subject: [PATCH] Script to set retention for all mailboxes in a tenant (#2798) The script requires authenticating with Exchange admin user name and password and will set retention to 0 for all tenant mailboxes. This helps with CI cleanup of test data The script can be integrated with some low frequency invocation with the CI pipeline to make sure the retention is properly set. Would require setting the credentials as Git secrets --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [x] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/cmd/purge/scripts/setRetention.ps1 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/cmd/purge/scripts/setRetention.ps1 diff --git a/src/cmd/purge/scripts/setRetention.ps1 b/src/cmd/purge/scripts/setRetention.ps1 new file mode 100644 index 000000000..d50d861ad --- /dev/null +++ b/src/cmd/purge/scripts/setRetention.ps1 @@ -0,0 +1,26 @@ +# This is tested on Mac as well as Docker (with m365pnp/powershell image) +# To run in Docker with the script in the current working diredctory +# docker run --rm -it -v "$(pwd):/usr/reset-retnention" -e M365TENANT_ADMIN_USER -e M365TENANT_ADMIN_PASSWORD \ +# -w /usr/reset-retnention m365pnp/powershell pwsh -c "./setRetention.ps1" +Param ( + [Parameter(Mandatory = $False, HelpMessage = "Exchange Admin email")] + [String]$AdminUser = $ENV:M365TENANT_ADMIN_USER, + + [Parameter(Mandatory = $False, HelpMessage = "Exchange Admin password")] + [String]$AdminPwd = $ENV:M365TENANT_ADMIN_PASSWORD +) + +# Setup ExchangeOnline +if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) { + Install-Module -Name ExchangeOnlineManagement -MinimumVersion 3.0.0 -Force +} + +Write-Host "Connecting to Exchange..." +$password = convertto-securestring -String "$AdminPwd" -AsPlainText -Force +$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminUser, $password +Connect-ExchangeOnline -Credential $cred + +Write-Host "Resetting retention..." +# Set retention values for all mailboxes +Get-Mailbox | ForEach-Object { Set-Mailbox -Identity $_.Alias -RetentionHoldEnabled $false -LitigationHoldEnabled $false -SingleItemRecoveryEnabled $false -RetainDeletedItemsFor 0 -AuditLogAgeLimit 0 -Force } +Get-Mailbox | ForEach-Object { Start-ManagedFolderAssistant -Identity $_.Alias }