Microsoft Graph ist eine Schnittstelle zu den Microsoft-365-Daten. Sie ermöglicht es, Apps zu erstellen, die mit Microsoft 365 arbeiten. Auf diese Weise ist der API-Zugriff direkt aus Programmiersprachen oder Skriptsprachen wie PowerShell möglich.
Hier wird ein Beispiel für den Zugriff auf Exchange-Online-Postfächer über diese Methode erläutert.
Ein „Microsoft-Arbeits- oder Schulkonto“ wird benötigt.
Install-Module Microsoft.Graph -Scope AllUsers
Get-InstalledModule Microsoft.Graph
$AppName = 'Test Graph'
#SignInAudience kann folgende Werte haben: "AzureADMyOrg", "AzureADMultipleOrgs","AzureADandPersonalMicrosoftAccount", "PersonalMicrosoftAccount"
$audience= "AzureADandPersonalMicrosoftAccount"
# Mandant für die Authentifizierung.
$authTenant = switch ($audience)
{
"AzureADMyOrg" { "tenantId" }
"AzureADMultipleOrgs" { "organizations" }
"AzureADandPersonalMicrosoftAccount" { "common" }
"PersonalMicrosoftAccount" { "consumers" }
default { "invalid" }
}
if ($authTenant -eq "invalid")
{
Write-Host -ForegroundColor Red "Ungültige Anmeldeumgebung:" $audience
Exit
}
# Erfordert einen Administrator
Connect-MgGraph -Scopes "Application.ReadWrite.All User.Read" -UseDeviceAuthentication -ErrorAction Stop
# Kontext für Zugriff auf Tenant-ID abrufen
$context = Get-MgContext -ErrorAction Stop
if ($authTenant -eq "tenantId")
{
$authTenant = $context.TenantId
}
# App-Registrierung erstellen
$appRegistration = New-MgApplication -DisplayName $AppName -SignInAudience $audience -IsFallbackPublicClient -ErrorAction Stop
Write-Host -ForegroundColor Cyan "App-Registrierung erstellt mit App-ID" $appRegistration.AppId
# Entsprechenden Dienstprinzipal erstellen
if ($audience-ne "PersonalMicrosoftAccount")
{
New-MgServicePrincipal -AppId $appRegistration.AppId -ErrorAction SilentlyContinue `
-ErrorVariable SPError | Out-Null
if ($SPError)
{
Write-Host -ForegroundColor Red "Ein Dienstprinzipal für die App konnte nicht erstellt werden."
Write-Host -ForegroundColor Red $SPError
Exit
}
Write-Host -ForegroundColor Cyan "Dienstprinzipal erstellt"
}
Write-Host
Write-Host -ForegroundColor Green "ERFOLG"
Write-Host -ForegroundColor Cyan -NoNewline "Client-ID: "
Write-Host -ForegroundColor Yellow $appRegistration.AppId
Write-Host -ForegroundColor Cyan -NoNewline "Auth-Tenant: "
Write-Host -ForegroundColor Yellow $authTenant
Disconnect-MgGraph
Write-Host "Verbindung zu Microsoft Graph getrennt"
Die App-Registrierung ist wie eine Vorlage für die eigentliche App, die als Enterprise-App bezeichnet wird. Enterprise-Apps sind „Instanzen“ von App-Registrierungen. Jede Enterprise-App verweist auf genau eine App-Registrierung, aber eine App-Registrierung kann für mehrere Enterprise-Apps verwendet werden.
Registriere und konfiguriere die App im Microsoft Azure Admin Center!
Option | Wer kann sich anmelden?
- Nur Konten in diesem Organisationsverzeichnis | Nur Benutzer in deiner Microsoft-365-Organisation
- Konten in einem beliebigen Organisationsverzeichnis | Benutzer in jeder Microsoft-365-Organisation (Arbeits- oder Schulkonten)
- Konten in einem beliebigen Organisationsverzeichnis ... und persönliche Microsoft-Konten | Benutzer in jeder Microsoft-365-Organisation (Arbeits- oder Schulkonten) und persönliche Microsoft-Konten
Über das Entra Admin Center → Anwendungen → Enterprise-Anwendungen → Neue Anwendung → Eigene Anwendung erstellen → „Registriere eine Anwendung zur Integration mit Microsoft Entra ID“ → Unterstützte Kontotypen = „Konten in einem beliebigen Organisationsverzeichnis“ (URI leer lassen)
#App-ID / Client-ID
$clientId = "<ClientID/AppID deiner App>"
#Tenant-ID
$authTenant = "<deineTenantID>"
#Scopes
$graphScopes = @("user.read", "mail.read", "mail.send")
# Benutzer authentifizieren
Connect-MgGraph -ClientId $clientId -TenantId $authTenant -Scopes $graphScopes -UseDeviceAuthentication
# Kontext abrufen
Get-MgContext
$context = Get-MgContext
# Authentifizierten Benutzer abrufen
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
Write-Host "Hallo," $user.DisplayName
Write-Host "E-Mail:", ($user.Mail + ' ' + $user.UserPrincipalName)
Get-MgUserMailFolderMessage -UserId $user.Id -MailFolderId Inbox -Select `
"from,isRead,receivedDateTime,subject" -OrderBy "receivedDateTime DESC" `
-Top 25 | Format-Table Subject,@{n='Von';e={$_.From.EmailAddress.Name}}, `
IsRead,ReceivedDateTime
$sendMailParams = @{
Message = @{
Subject = "Test Microsoft Graph"
Body = @{
ContentType = "text"
Content = "Hallo Welt!"
}
ToRecipients = @(
@{
EmailAddress = @{
Address = ("example@example.com")
}
}
)
}
}
Send-MgUserMail -UserId $user.Id -BodyParameter $sendMailParams
# Verbindung trennen
Disconnect-MgGraph
Das folgende Skript entfernt die Enterprise-Anwendung UND die App-Registrierung, um den Test aufzuräumen
# Erfordert einen Azure-Administrator
Connect-MgGraph -Scopes "Application.ReadWrite.All User.Read" -UseDeviceAuthentication -ErrorAction Stop
# Hinweis! -> Ein "Service Principal Object" ist hier dasselbe wie eine "Enterprise-Anwendung"
$app = Get-MgServicePrincipal -Filter "DisplayName eq 'Test Graph'"
Remove-MgServicePrincipal -ServicePrincipalId $app.Id
Remove-MgApplicationByAppId -AppId $app.AppId
Disconnect-MgGraph
Write-Host "Verbindung zu Microsoft Graph getrennt"
H@ppy H@cking