125 views +0 -0

Entra app-registratie Mail.Send

# Module (once): Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Application.ReadWrite.All","AppRoleAssignment.ReadWrite.All","DelegatedPermissionGrant.ReadWrite.All","User.Read.All","Group.Read.All"

# ===== Variables =====================================================
$appName      = "Website WP Mail SMTP"
$redirectUris = @("https://REDIRECT-URL-UIT-DE-PLUGIN")   # exactly as the plugin shows it
$audience     = "AzureADandPersonalMicrosoftAccount"      # or "AzureADMyOrg" / "AzureADMultipleOrgs"
$scopes       = "Mail.Send","offline_access","User.Read"  # delegated Graph permissions
$assignTo     = "website@example.com"                     # UPN, group name, or "" for everyone
$secretMonths = 24
# =====================================================================

# Look up who may sign in first, so a typo fails before anything is created
$principal = $null
if ($assignTo) {
  if ($assignTo -like "*@*") {
    $principal = Get-MgUser -UserId $assignTo -ErrorAction Stop
  } else {
    $principal = Get-MgGroup -Filter "displayName eq '$assignTo'"
    if (@($principal).Count -ne 1) { throw "Expected exactly one group named '$assignTo'" }
  }
}

# Check the scopes exist before creating anything
$graphSp = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'"
$access  = $graphSp.Oauth2PermissionScopes | Where-Object { $_.Value -in $scopes }
$missing = $scopes | Where-Object { $_ -notin $access.Value }
if ($missing) { throw "Unknown Graph scope(s): $($missing -join ', ')" }

if (Get-MgApplication -Filter "displayName eq '$appName'") {
  throw "An app named '$appName' already exists"
}

# 1. App registration with Web redirect URI(s)
$appParams = @{
  DisplayName    = $appName
  SignInAudience = $audience
  Web            = @{ RedirectUris = $redirectUris }
  RequiredResourceAccess = @(@{
    ResourceAppId  = $graphSp.AppId
    ResourceAccess = @($access | ForEach-Object { @{ Id = $_.Id; Type = "Scope" } })
  })
}
# Personal accounts require v2 access tokens
if ($audience -like "*Personal*") { $appParams.Api = @{ RequestedAccessTokenVersion = 2 } }
$app = New-MgApplication @appParams

# 2. Enterprise app; Assignment required only when $assignTo is set
$sp = New-MgServicePrincipal -AppId $app.AppId -AppRoleAssignmentRequired:([bool]$assignTo)

# 3. Admin consent for the whole tenant
New-MgOauth2PermissionGrant -ClientId $sp.Id -ConsentType "AllPrincipals" `
  -ResourceId $graphSp.Id -Scope ($scopes -join " ") | Out-Null

# 4. Assign user or group (default access)
if ($principal) {
  New-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $sp.Id `
    -PrincipalId $principal.Id -ResourceId $sp.Id -AppRoleId ([guid]::Empty) | Out-Null
}

# 5. Client secret
$secret = Add-MgApplicationPassword -ApplicationId $app.Id -PasswordCredential @{
  DisplayName = "$appName $($secretMonths)m"; EndDateTime = (Get-Date).AddMonths($secretMonths)
}

# 6. Values to hand over; the secret is shown only this once
[pscustomobject]@{
  TenantId     = (Get-MgContext).TenantId
  ClientId     = $app.AppId
  ClientSecret = $secret.SecretText
  SecretExpiry = $secret.EndDateTime
  RedirectUris = $redirectUris -join ", "
  AssignedTo   = if ($assignTo) { $assignTo } else { "everyone" }
} | Format-List