<# .SYNOPSIS LeaverGuard check (READ-ONLY): finds OneDrive and shared mailbox data at risk. .DESCRIPTION *** THIS SCRIPT IS READ-ONLY. IT MAKES NO CHANGES TO YOUR TENANT. *** It only uses Get- cmdlets (plus Connect-/Disconnect- to sign in). It does not call any Set-, New-, Remove-, Enable- or Start- cmdlet, and it does not read email or open any file. It writes one file, leaverguard.json, on this computer. Nothing is uploaded. You choose whether to open it on the LeaverGuard page, where it is read inside your browser. What it reads: - Get-SPOSite -IncludePersonalSite $true -Limit All personal (OneDrive) sites: Url, Owner, StorageUsageCurrent (MB), LastContentModifiedDate, LockState - Get-SPOSite ... -ArchiveStatus which OneDrives are archived (tried for each status; skipped with a note if your module does not support it) - Get-EXOMailbox -RecipientTypeDetails SharedMailbox shared mailboxes: ArchiveStatus, LitigationHoldEnabled, SkuAssigned - Get-EXOMailboxStatistics shared mailbox size - Get-AcceptedDomain only to work out your SharePoint admin address (the *.onmicrosoft.com name) .REQUIREMENTS Install-Module ExchangeOnlineManagement -Scope CurrentUser Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser (Windows PowerShell 5.1, or PowerShell 7 with -UseWindowsPowerShell) An account with read access: SharePoint Administrator or Global Reader (SharePoint) and a read-only Exchange role such as Global Reader or View-Only Organization Management. .EXAMPLE .\LeaverGuard-Check.ps1 .\LeaverGuard-Check.ps1 -TenantName contoso -OutFile C:\Temp\leaverguard.json #> param( [string]$TenantName = "", # the part before .onmicrosoft.com; found automatically if left empty [string]$OutFile = ".\leaverguard.json" ) $ErrorActionPreference = 'Stop' Write-Host "" Write-Host "=====================================================================" -ForegroundColor Green Write-Host " LeaverGuard check: READ-ONLY. This script makes NO changes." -ForegroundColor Green Write-Host " It only reads, and saves one file on this computer: $OutFile" -ForegroundColor Green Write-Host "=====================================================================" -ForegroundColor Green Write-Host "" foreach ($m in 'ExchangeOnlineManagement', 'Microsoft.Online.SharePoint.PowerShell') { if (-not (Get-Module -ListAvailable -Name $m)) { throw "The module $m is not installed. Run: Install-Module $m -Scope CurrentUser" } } Import-Module ExchangeOnlineManagement Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking $notes = New-Object System.Collections.Generic.List[string] function ConvertTo-ByteCount($value) { # TotalItemSize arrives as "1.2 GB (1,288,490,188 bytes)" over remote PowerShell, or as an object with .Value if ($null -eq $value) { return $null } $text = [string]$value if ($text -match '\(([\d,\.\s]+) bytes\)') { return [int64](($Matches[1]) -replace '[^\d]', '') } try { if ($value.Value) { return [int64]$value.Value.ToBytes() } } catch { } return $null } # ---------------- Exchange Online: shared mailboxes ---------------- Write-Host "Signing in to Exchange Online..." -ForegroundColor Cyan Connect-ExchangeOnline -ShowBanner:$false if (-not $TenantName) { $initial = Get-AcceptedDomain | Where-Object { $_.InitialDomain } | Select-Object -First 1 if ($initial) { $TenantName = ($initial.DomainName -split '\.')[0] } } if (-not $TenantName) { throw "Could not work out your tenant name. Run again with -TenantName contoso" } Write-Host "Reading shared mailboxes..." -ForegroundColor Cyan $shared = @() try { $boxes = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited -Properties ArchiveStatus, ArchiveGuid, LitigationHoldEnabled, SkuAssigned } catch { $notes.Add("Get-EXOMailbox -Properties failed ($($_.Exception.Message)); used the default property set, so archive, litigation hold and licence flags may be missing.") $boxes = Get-EXOMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited } $i = 0 foreach ($b in $boxes) { $i++ if ($i % 25 -eq 0) { Write-Host " $i shared mailboxes..." } $bytes = $null try { $stats = Get-EXOMailboxStatistics -Identity $b.UserPrincipalName -ErrorAction Stop $bytes = ConvertTo-ByteCount $stats.TotalItemSize } catch { $notes.Add("No size for $($b.UserPrincipalName): $($_.Exception.Message)") } $shared += [pscustomobject]@{ upn = $b.UserPrincipalName displayName = $b.DisplayName totalItemSizeBytes = $bytes archiveStatus = [string]$b.ArchiveStatus archiveGuid = [string]$b.ArchiveGuid litigationHoldEnabled = $b.LitigationHoldEnabled skuAssigned = $b.SkuAssigned } } Write-Host " Found $($shared.Count) shared mailboxes." -ForegroundColor Green # ---------------- SharePoint Online: OneDrive (personal) sites ---------------- Write-Host "Signing in to SharePoint Online (https://$TenantName-admin.sharepoint.com)..." -ForegroundColor Cyan Connect-SPOService -Url "https://$TenantName-admin.sharepoint.com" Write-Host "Reading OneDrive sites (this can take a few minutes)..." -ForegroundColor Cyan $sites = Get-SPOSite -IncludePersonalSite $true -Limit All | Where-Object { $_.Url -like '*-my.sharepoint.com/personal/*' } Write-Host " Found $(@($sites).Count) OneDrive sites." -ForegroundColor Green # Archive status: filter the same listing by each status (the property is not in the default output). $archive = @{} $archiveWorked = $true foreach ($status in 'Archived', 'FullyArchived', 'RecentlyArchived', 'Reactivating') { try { foreach ($s in (Get-SPOSite -IncludePersonalSite $true -Limit All -ArchiveStatus $status)) { $archive[$s.Url] = $status } } catch { $archiveWorked = $false $notes.Add("Get-SPOSite -ArchiveStatus $status failed: $($_.Exception.Message). Archive status is missing; check Reports > OneDrive accounts in the SharePoint admin center.") break } } $drives = @() foreach ($s in $sites) { $arch = if ($archive.ContainsKey($s.Url)) { $archive[$s.Url] } elseif ($archiveWorked) { 'NotArchived' } else { $null } $drives += [pscustomobject]@{ url = $s.Url owner = $s.Owner storageBytes = [int64]$s.StorageUsageCurrent * 1MB # StorageUsageCurrent is in megabytes lastContentModified = if ($s.LastContentModifiedDate) { ([datetime]$s.LastContentModifiedDate).ToUniversalTime().ToString('o') } else { $null } lockState = [string]$s.LockState archiveStatus = $arch } } # ---------------- write the file ---------------- $result = [pscustomobject]@{ tool = 'LeaverGuard-Check' version = 1 generated = (Get-Date).ToUniversalTime().ToString('o') oneDrives = $drives sharedMailboxes = $shared notes = $notes } $result | ConvertTo-Json -Depth 5 | Set-Content -Path $OutFile -Encoding UTF8 Disconnect-ExchangeOnline -Confirm:$false | Out-Null try { Disconnect-SPOService | Out-Null } catch { } Write-Host "" Write-Host "Done. Wrote $OutFile ($($drives.Count) OneDrives, $($shared.Count) shared mailboxes)." -ForegroundColor Green if ($notes.Count) { Write-Host "Notes:" -ForegroundColor Yellow; $notes | ForEach-Object { Write-Host " - $_" -ForegroundColor Yellow } } Write-Host "Open the LeaverGuard page and upload this file. Nothing was changed in your tenant."