functions/azure/Get-MicrosoftGraphServicePrincipalId.ps1

function Get-MicrosoftGraphServicePrincipalId {
    $headers = @{
        Authorization = Get-RequestHeaderAuthorization -RequestUri 'https://graph.microsoft.com'
        "Content-Type" = "application/json"
    }

    $graphAppId = Get-KnownResourceAppId -MicrosoftGraph
    $graphSpUri = "https://graph.microsoft.com/v1.0/servicePrincipals?`$filter=appId eq '$graphAppId'"
    $graphSp = Invoke-RestMethod -Method GET -Uri $graphSpUri -Headers $headers
    if (-not $graphSp.value) {
        Write-Error "Unable to retrieve Microsoft Graph Service Principal!"
        return $false
    }

    return [guid]$graphSp.value[0].id
}