Get-MailboxPermissionReport.ps1
|
<#PSScriptInfo
.VERSION 1.0.0 .GUID bf447504-f3d5-5e36-b03a-e012f6b78a45 .AUTHOR Torsten Weise .COMPANYNAME powerscripting.de .COPYRIGHT (c) 2026 Torsten Weise. MIT-Lizenz. .TAGS Powerscripting German Exchange ExchangeOnline Mailbox PSEdition_Desktop PSEdition_Core .LICENSEURI https://powerscripting.de/skripte/files/LICENSE.txt .PROJECTURI https://powerscripting.de/skripte/get-mailboxpermissionreport.html .ICONURI https://powerscripting.de/assets/icon-192.png .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Erste Veröffentlichung in der PowerShell Gallery. .PRIVATEDATA #> <# .SYNOPSIS Zeigt, wer auf welche Postfächer Vollzugriff hat und wer in wessen Namen senden darf. .DESCRIPTION Postfachrechte sammeln sich über Jahre an: die Vertretung aus dem Urlaub 2021, der Azubi mit Zugriff auf die Buchhaltung, der ausgeschiedene Teamleiter, der immer noch das Funktionspostfach sieht. Das Skript listet alle ausdrücklich vergebenen Rechte: - Vollzugriff (FullAccess): lesen, löschen, Ordner anlegen. Das Postfach erscheint per Automapping in Outlook. - Senden als (SendAs): Mail geht mit der Adresse des Postfachs raus, der Empfänger sieht keinen Unterschied. - Senden im Auftrag (SendOnBehalf): Mail zeigt "X im Auftrag von Y". Geerbte Rechte, Systemkonten (NT AUTHORITY\SELF, Exchange-Server, Discovery Management) und Rechte, die jemand auf sein eigenes Postfach hat, blendet das Skript aus. Deaktivierte oder gelöschte Benutzer, die noch Rechte haben, werden markiert. Das Skript erkennt selbst, ob es in Exchange Online (nach Connect-ExchangeOnline) oder in der Exchange Management Shell eines lokalen Servers läuft, und nutzt die passenden Befehle. .PARAMETER Mailbox Optional. Nur diese Postfächer prüfen. Standard: alle Benutzer-, freigegebenen und Raumpostfächer. .PARAMETER User Optional. Nur Rechte dieses Benutzers zeigen, etwa vor dem Austritt eines Mitarbeiters. .PARAMETER ReportPath Ordner für HTML- und CSV-Bericht. Standard: aktueller Ordner. .EXAMPLE .\Get-MailboxPermissionReport.ps1 Listet alle Postfachrechte im Unternehmen. .EXAMPLE .\Get-MailboxPermissionReport.ps1 -User max.mustermann@contoso.de Zeigt, auf welche Postfächer Max zugreifen und in wessen Namen er senden kann. .EXAMPLE .\Get-MailboxPermissionReport.ps1 -Mailbox info@contoso.de, buchhaltung@contoso.de Prüft gezielt zwei Funktionspostfächer. .NOTES Autor: Torsten Weise, powerscripting.de Exchange Online: Install-Module ExchangeOnlineManagement, danach Connect-ExchangeOnline. Exchange Server 2016, 2019 oder SE: in der Exchange Management Shell starten. Bei großen Umgebungen dauert die Abfrage, weil jedes Postfach einzeln gelesen wird. Das Skript liest nur und ändert nichts. #> [CmdletBinding()] param( [string[]] $Mailbox, [string] $User, [string] $ReportPath = (Get-Location).Path ) $online = [bool](Get-Command Get-EXOMailbox -ErrorAction SilentlyContinue) -and [bool](Get-ConnectionInformation -ErrorAction SilentlyContinue) if (-not $online -and -not (Get-Command Get-MailboxPermission -ErrorAction SilentlyContinue)) { throw 'Keine Exchange-Sitzung gefunden. Erst Connect-ExchangeOnline ausführen oder die Exchange Management Shell starten.' } Write-Host ("Umgebung: {0}" -f $(if ($online) { 'Exchange Online' } else { 'Exchange Server' })) $skip = '^(NT AUTHORITY\\|NT-AUTORITÄT\\|S-1-5-|.*\\Exchange (Servers|Trusted Subsystem|Windows Permissions|Domain Servers)|.*\\Organization Management|.*\\Discovery Management|.*\\Administrator$|.*\\Domain Admins|.*\\Enterprise Admins|.*\\Organisations-Admins|.*\\Domänen-Admins|.*\\Managed Availability Servers|.*\\Public Folder Management|.*\\Delegated Setup|.*\\Exchange Public Folder Administrators)' # Empfänger einmal auflösen und zwischenspeichern $cache = @{} function Resolve-Trustee([string] $Name) { if ($cache.ContainsKey($Name)) { return $cache[$Name] } $r = $null try { $r = if ($online) { Get-EXORecipient -Identity $Name -ErrorAction Stop -Properties RecipientTypeDetails } else { Get-Recipient -Identity $Name -ErrorAction Stop } } catch { } $info = if ($r) { $state = '' if ($r.RecipientTypeDetails -eq 'UserMailbox' -or $r.RecipientTypeDetails -like '*User') { try { $u = Get-User -Identity $r.Identity -ErrorAction Stop # Exchange Online liefert AccountDisabled, Exchange Server UserAccountControl if ($u.AccountDisabled -or ([string]$u.UserAccountControl -match 'AccountDisabled')) { $state = 'Konto deaktiviert' } } catch { } } [pscustomobject]@{ Name = $r.DisplayName; Address = [string]$r.PrimarySmtpAddress; State = $state } } else { [pscustomobject]@{ Name = $Name; Address = ''; State = 'nicht auflösbar (gelöscht?)' } } $cache[$Name] = $info return $info } $types = 'UserMailbox', 'SharedMailbox', 'RoomMailbox', 'EquipmentMailbox' $boxes = if ($Mailbox) { foreach ($m in $Mailbox) { if ($online) { Get-EXOMailbox -Identity $m -Properties GrantSendOnBehalfTo, RecipientTypeDetails } else { Get-Mailbox -Identity $m } } } elseif ($online) { Get-EXOMailbox -ResultSize Unlimited -RecipientTypeDetails $types -Properties GrantSendOnBehalfTo, RecipientTypeDetails } else { Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails $types } $boxes = @($boxes) Write-Host "$($boxes.Count) Postfächer werden gelesen" $userInfo = if ($User) { Resolve-Trustee $User } else { $null } $rows = [System.Collections.Generic.List[object]]::new() $i = 0 foreach ($b in $boxes) { $i++ Write-Progress -Activity 'Postfachrechte' -Status $b.PrimarySmtpAddress -PercentComplete (100 * $i / $boxes.Count) $id = [string]$b.PrimarySmtpAddress # Vollzugriff $full = if ($online) { Get-EXOMailboxPermission -Identity $id -ErrorAction SilentlyContinue } else { Get-MailboxPermission -Identity $id -ErrorAction SilentlyContinue } foreach ($p in @($full | Where-Object { -not $_.IsInherited -and -not $_.Deny -and $_.AccessRights -match 'FullAccess' })) { $t = [string]$p.User if ($t -match $skip) { continue } $rows.Add([pscustomobject]@{ Postfach = $id; Typ = $b.RecipientTypeDetails; Recht = 'Vollzugriff'; Trustee = $t }) } # Senden als $sendAs = if ($online) { Get-EXORecipientPermission -Identity $id -ErrorAction SilentlyContinue | Where-Object { $_.AccessRights -contains 'SendAs' -and -not $_.IsInherited } | ForEach-Object { $_.Trustee } } else { Get-ADPermission -Identity $b.DistinguishedName -ErrorAction SilentlyContinue | Where-Object { $_.ExtendedRights -like '*Send-As*' -and -not $_.IsInherited -and -not $_.Deny } | ForEach-Object { $_.User } } foreach ($t in @($sendAs)) { $t = [string]$t if (-not $t -or $t -match $skip) { continue } $rows.Add([pscustomobject]@{ Postfach = $id; Typ = $b.RecipientTypeDetails; Recht = 'Senden als'; Trustee = $t }) } # Senden im Auftrag foreach ($t in @($b.GrantSendOnBehalfTo)) { if ($t) { $rows.Add([pscustomobject]@{ Postfach = $id; Typ = $b.RecipientTypeDetails; Recht = 'Senden im Auftrag'; Trustee = [string]$t }) } } } Write-Progress -Activity 'Postfachrechte' -Completed # Trustees auflösen, eigene Rechte und Filter anwenden $result = foreach ($r in $rows) { $ti = Resolve-Trustee $r.Trustee if ($ti.Address -and $ti.Address -eq $r.Postfach) { continue } if ($userInfo -and $ti.Address -ne $userInfo.Address -and $r.Trustee -ne $User) { continue } [pscustomobject]@{ Postfach = $r.Postfach Typ = $r.Typ Recht = $r.Recht Benutzer = $ti.Name Adresse = $ti.Address Hinweis = $ti.State } } $result = @($result | Sort-Object @{ e = { -not $_.Hinweis } }, Postfach, Recht, Benutzer) $result $stale = @($result | Where-Object Hinweis).Count $stats = $result | Group-Object Recht | ForEach-Object { "$($_.Name): $($_.Count)" } Write-Host '' Write-Host (($stats + "Rechte von deaktivierten oder gelöschten Konten: $stale") -join ' | ') $now = Get-Date $title = "Postfachrechte - $($now.ToString('dd.MM.yyyy'))" $css = '<style>body{font-family:Segoe UI,Arial,sans-serif;font-size:13px}table{border-collapse:collapse}td,th{border:1px solid #ccc;padding:4px 8px}th{background:#eee}</style>' $scope = if ($User) { " für $User" } else { '' } $pre = "<h2>$title$scope</h2><p>$($boxes.Count) Postfächer gelesen, $($result.Count) Rechte. $($stats -join ', '). Deaktiviert oder gelöscht: $stale.</p>" $html = $result | ConvertTo-Html -Title $title -Head $css -PreContent $pre | Out-String $html = $html -replace '<td>(Konto deaktiviert|nicht auflösbar \(gelöscht\?\))</td>', '<td style="background:#f8d7da">$1</td>' $base = Join-Path $ReportPath ("Postfachrechte_{0:yyyy-MM-dd}" -f $now) $html | Set-Content -Path "$base.html" -Encoding UTF8 $result | Export-Csv -Path "$base.csv" -NoTypeInformation -Delimiter ';' -Encoding UTF8 Write-Host "Bericht: $base.html und $base.csv" |