Invoke REST Method with PowerShell¶
$token = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/").Token
$uri = "https://graph.microsoft.com/v1.0/me"
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
Try {
$result = Invoke-RestMethod -Uri $uri -Method "GET" -Headers $headers
}
Catch {
$result = $null
$errResp = $_
}
If ($result) {
Write-Host "Microsoft Graph call successessful. Output: $($result | Format-List | Out-String)"
}
Else {
Write-Error "There was an issue calling Microsoft Graph. Error: $($errResp | Format-List | Out-String)"
}
You could also invoke POST method:
# ...
$body = @{
"DisplayName" = "${{ parameters.servicePrincipalName }}"
} | ConvertTo-Json -Compress
Try {
$result = Invoke-RestMethod -Uri $uri -Method "Post" -Headers $headers -Body $body
}
# ...