The error you're encountering, "InvalidResourceIdSegment," suggests that there's an issue with how the resource ID is being constructed. In your case, it seems that the inclusion of the "id" field in the request body might be causing the problem. Try below?
function SetSqlServerActiveDirectoryAdministrator(
[string]$resourceGroupName,
[string]$sqlServerName,
[string]$adminDisplayName,
[string]$adminId,
[string]$tenantId,
[string]$subscriptionId
) {
$url = "https://graph.microsoft.com/v1.0/myorganization/servicePrincipals/$adminId/appRoleAssignments?api-version=1.6"
Write-Host $url
$token = (Get-AzAccessToken).Token
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
"Accept" = "application/json, */*; q=0.01"
}
$Body = @{
"principalId" = $adminId
"resourceId" = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$sqlServerName"
"appRoleId" = "00000000-0000-0000-0000-000000000000" # This is the constant GUID for the "Admin" role in SQL
}
Invoke-RestMethod -Method 'Post' -Uri $url -Headers $headers -Body ($Body | ConvertTo-Json)
}