# Funktion zum Abruf des API-Tokens function Get-GraphAPIToken { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string]$tenantId, [Parameter(Mandatory=$true)] [string]$clientId, [Parameter(Mandatory=$true)] [string]$password ) $tokenUri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" # Body für die Token-Anfrage $body = @{ client_id = $clientId scope = "https://graph.microsoft.com/.default" client_secret = $password grant_type = "client_credentials" } try { # Abrufen des Access-Tokens $response = Invoke-RestMethod -Method Post -Uri $tokenUri -ContentType "application/x-www-form-urlencoded" -Body $body $accessToken = $response.access_token return $accessToken } catch { Write-Error "Fehler beim Abrufen des Access Tokens: $_" Read-Host "Fehler. Beende Skript." exit 1 } } # --------------------------- # Konfiguration: API-Zugangsdaten # --------------------------- #Datemimport $importData = Get-Content -Path "$PSScriptRoot\config.json" | ConvertFrom-Json if($importData -eq $null){ Write-Error "Fehler beim Aufrufen von config.json" Read-Host "Fehler. Beende Skript." exit 1 } $tenantId = $importData.TenantID # Ersetze durch deine Tenant-ID (aus EntraID > Overview) $clientId = $importData.ClientID # Ersetze durch deine Client-ID (aus der App-Registration > GraphAPI) $password = $importData.ClientPassword # Ersetze durch das generierte Client-Secret (aus dem vorher gespeichertem VALUE) if($tenantId -eq "" -or $clientId -eq "" -or $password -eq ""){ Write-Error "Bitte überprüfen Sie die config.json!" Read-Host "Fehler. Skript beendet." exit 1 } # Abrufen des Tokens $token = Get-GraphAPIToken -tenantId $tenantId -clientId $clientId -password $password if (-not $token) { Write-Error "Kein gültiger Access Token erhalten. Abbruch." Read-Host "Fehler. Beende Skript." exit 1 } # --------------------------- # Graph API - AuthorizationPolicy updaten # --------------------------- # Endpoint wie in der Anleitung $graphAPIUrl = "https://graph.microsoft.com/v1.0/policies/authorizationPolicy" # JSON-Body, um allowEmailVerifiedUsersToJoinOrganization auf False zu setzen $updateBody = @{ allowEmailVerifiedUsersToJoinOrganization =$false } | ConvertTo-Json $headers=@{ "Authorization" = "Bearer $token" "Content-Type" = "application/json" } try { # PATCH-Anfrage zur Aktualisierung der Policy $result = Invoke-WebRequest -Uri $graphAPIUrl -Method Patch -Headers $headers -Body $updateBody Write-Host "Die AuthorizationPolicy wurde erfolgreich aktualisiert." Write-Output $result } catch { Write-Error "Fehler beim Aktualisieren der AuthorizationPolicy: $_" } try { # PATCH-Anfrage zur Aktualisierung der Policy $result = Invoke-WebRequest -Uri $graphAPIUrl -Method GET -Headers $headers Write-Host "Die AuthorizationPolicy wurde erfolgreich aktualisiert." Write-Output $result } catch { Write-Error "Fehler beim Aktualisieren der AuthorizationPolicy: $_" } Read-Host "Das Skript ist beendet"