是否有等效于 "az rest" 的 PowerShell?

Is there a PowerShell equivalent to "az rest"?

我最近发现了 az rest 命令,它允许我执行经过身份验证的 REST 命令,而不必担心获取令牌。

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\"}"

Azure Powershell 中是否有等效项?我需要进行一个无法通过任何 AzAd... cmdlet 调用的调用。我会想象像 Invoke-AzRestMethod 这样的东西,但这并不存在。

编辑:我想执行无法通过 Azure AD Cmdlet 执行的调用(目前)。喜欢直接使用新的 ,或为 AAD B2C(Beta API)上传自定义策略。

目前没有内置的 powershell 命令等于 az rest

我的解决方法是使用下面的命令,您可以简单地使用它通过您的登录 account/service 主体获取特定资源的特定令牌,例如https://management.azure.comhttps://graph.microsoft.com,也可以是其他资源,甚至是你在AAD中自定义API的app-id

示例:

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

解码token,发现audience是正确的

拿到token后,可以直接使用Invoke-RestMethod to call any REST API you want, for the format, you can check this .

您现在可以使用 Az Powershell 模块执行此操作

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

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