Alex
Alex

Reputation: 18546

Is there a PowerShell equivalent to "az rest"?

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/[email protected]" --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).

Upvotes: 7

Views: 3973

Answers (2)

alastairtree
alastairtree

Reputation: 4289

You can now do this with the Az Powershell module

Invoke-AzRestMethod
      -Path <String>
      -Method <String>
      [-Payload <String>]
      [-AsJob]
      [-DefaultProfile <IAzureContextContainer>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

https://learn.microsoft.com/en-us/powershell/azure/manage-azure-resources-invoke-azrestmethod?view=azps-5.9.0

Upvotes: 9

Joy Wang
Joy Wang

Reputation: 42063

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

enter image description here

Decode the token, we can find the audience is correct.

enter image description here

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.

Upvotes: 3

Related Questions