问题
I recently discovered the az rest
command, which allows me to perform authenticated REST commands, without having to worry about acquiring tokens.
https://www.codeisahighway.com/native-azure-rest-api-calls-now-available-in-azure-cli-2-0-67/
az rest --method patch --url "https://graph.microsoft.com/v1.0/users/johndoe@azuresdkteam.onmicrosoft.com" --body "{\"displayName\": \"jondoe2\"}"
Is there an equivalent in Azure Powershell? I need to do a call which is not available via any of the AzAd...
cmdlets. I would imagine something like Invoke-AzRestMethod
, but this does not exist.
Edit: I want to execute calls which are not available via the Azure AD Cmdlets (yet). Like using the new typed replyUrls directly, or uploading custom policies for AAD B2C (Beta API).
回答1:
There is no built-in powershell command equals az rest
currently.
My workaround is to use the command below, you could simply use it to get the specific token for a specific resource with your login account/service principal, e.g. https://management.azure.com
, https://graph.microsoft.com
, it can also be other resources, even the app-id
of your custom API in AAD.
Sample:
Connect-AzAccount
$resource = "https://graph.microsoft.com"
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$Token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken
Decode the token, we can find the audience
is correct.
After getting the token, you can simply use the Invoke-RestMethod to call any REST API you want, for the format, you can check this sample.
来源:https://stackoverflow.com/questions/62690216/is-there-a-powershell-equivalent-to-az-rest