Setup-SMTPGraphRelay.ps1 aktualisiert

This commit is contained in:
2026-08-18 14:42:39 +02:00
parent 70afda86bf
commit 48a8f39990
+63 -27
View File
@@ -1,4 +1,4 @@
#Requires -Version 5.1 #Requires -Version 5.1
#Requires -RunAsAdministrator #Requires -RunAsAdministrator
<# <#
.SYNOPSIS .SYNOPSIS
@@ -901,14 +901,40 @@ function Install-New {
} }
Write-Ok "Entra Service Principal erstellt: $($sp.Id)" Write-Ok "Entra Service Principal erstellt: $($sp.Id)"
Test-NoGlobalMailSend -ServicePrincipalObjectId $sp.Id
$useRbac = Confirm-Yes "Soll der E-Mail-Versand auf das Postfach '$senderMailbox' per Exchange RBAC begrenzt werden (Nein = Tenant-weiter Versand erlaubt)?"
Ensure-ExchangeRbac ` if ($useRbac) {
-TenantId $tenantId ` Test-NoGlobalMailSend -ServicePrincipalObjectId $sp.Id
-ClientId $app.AppId ` Ensure-ExchangeRbac `
-ServicePrincipalObjectId $sp.Id ` -TenantId $tenantId `
-AppName $appName ` -ClientId $app.AppId `
-SenderMailbox $senderMailbox -ServicePrincipalObjectId $sp.Id `
-AppName $appName `
-SenderMailbox $senderMailbox
} else {
Write-Warn "Überspringe Exchange RBAC. Erteile stattdessen globale Mail.Send Berechtigung in Entra ID."
$graphSp = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'" -Property "id,appRoles"
$mailSendRole = $graphSp.AppRoles | Where-Object { $_.Value -eq "Mail.Send" -and $_.AllowedMemberTypes -contains "Application" } | Select-Object -First 1
if (-not $mailSendRole) {
throw "Microsoft Graph AppRole 'Mail.Send' konnte nicht gefunden werden."
}
$existingAssig = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $sp.Id -All -ErrorAction SilentlyContinue | Where-Object { $_.AppRoleId -eq $mailSendRole.Id }
if (-not $existingAssig) {
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $sp.Id `
-PrincipalId $sp.Id `
-ResourceId $graphSp.Id `
-AppRoleId $mailSendRole.Id | Out-Null
Write-Ok "Globale 'Mail.Send' Application Role (ohne Einschränkung) erfolgreich vergeben."
} else {
Write-Ok "Globale 'Mail.Send' Berechtigung war bereits vergeben."
}
}
$config = [ordered]@{ $config = [ordered]@{
Smtp = [ordered]@{ Smtp = [ordered]@{
@@ -1319,32 +1345,42 @@ function Verify-CloudRbac {
} }
Write-Ok "Entra Service Principal gefunden: $($sp.Id)" Write-Ok "Entra Service Principal gefunden: $($sp.Id)"
Test-NoGlobalMailSend -ServicePrincipalObjectId $sp.Id
$graphSp = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'" -Property "id,appRoles"
$mailSendRole = $graphSp.AppRoles | Where-Object { $_.Value -eq "Mail.Send" -and $_.AllowedMemberTypes -contains "Application" } | Select-Object -First 1
$globalAssig = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $sp.Id -All -ErrorAction SilentlyContinue | Where-Object { $_.AppRoleId -eq $mailSendRole.Id }
Import-Module ExchangeOnlineManagement -Force -ErrorAction Stop if ($globalAssig) {
Write-Info "Exchange Online Anmeldung erforderlich (Admin)." Write-Warn "App besitzt GLOBALE (tenant-weite) Microsoft Graph Mail.Send Berechtigung in Entra ID."
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop Write-Info "Eine Exchange RBAC Prüfung wird übersprungen, da die App ohnehin im Namen aller Postfächer senden darf."
} else {
Write-Ok "Keine globale Entra ID Berechtigung. Prüfe Exchange Application RBAC Einschränkung..."
Import-Module ExchangeOnlineManagement -Force -ErrorAction Stop
Write-Info "Exchange Online Anmeldung erforderlich (Admin)."
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop
try { try {
$auth = Test-ServicePrincipalAuthorization ` $auth = Test-ServicePrincipalAuthorization `
-Identity $sp.Id ` -Identity $sp.Id `
-Resource $config.Graph.SenderMailbox -Resource $config.Graph.SenderMailbox
$role = $auth | Where-Object { $_.RoleName -eq "Application Mail.Send" } | Select-Object -First 1 $role = $auth | Where-Object { $_.RoleName -eq "Application Mail.Send" } | Select-Object -First 1
if ($role -and $role.InScope) { if ($role -and $role.InScope) {
Write-Ok "Exchange Application Mail.Send: SenderMailbox ist InScope." Write-Ok "Exchange Application Mail.Send: SenderMailbox ist InScope."
if ($role.AllowedResourceScope) { if ($role.AllowedResourceScope) {
Write-Info "AllowedResourceScope: $($role.AllowedResourceScope)" Write-Info "AllowedResourceScope: $($role.AllowedResourceScope)"
}
}
else {
Write-Fail "Exchange Application Mail.Send fehlt oder SenderMailbox ist nicht InScope."
} }
} }
else { finally {
Write-Fail "Exchange Application Mail.Send fehlt oder SenderMailbox ist nicht InScope." Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
} }
} }
finally {
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
}
} }
finally { finally {
Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null Disconnect-MgGraph -ErrorAction SilentlyContinue | Out-Null
@@ -2299,4 +2335,4 @@ while ($true) {
if ($choice -eq "0") { if ($choice -eq "0") {
break break
} }
} }