使用 azure cli 获取 Azure API 管理订阅密钥

Fetch Azure API Management subscription key using azure cli

我正在尝试使用 cli 获取我的产品的订阅密钥以及默认订阅密钥。我已经阅读了文档 https://docs.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest ,但现在我没有看到任何命令来获取订阅密钥。

虽然我可以看到有 powershell 获取它的方法,但我们 运行 ubuntu 管道中的任务,下面列出的命令在 linux 代理中不起作用。它说 Set-AzContext 不是已知命令

$subscriptionId = "id"
$RG = "rg"
$service = "apim-name"

Set-AzContext -Subscription $subscriptionId 

$apimContext = New-AzApiManagementContext -ResourceGroupName $RG -ServiceName $service

Get-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId "master"

更新 我能够通过 DevOps 管道中 Azure 的 powershell 任务获取详细信息。如果 azure cli 中没有选项,我将使用它作为解决方法。

目前无法使用 AZ CLI 命令获取订阅密钥。使用的 PowerShell 命令是正确的方法。以防万一它是否有助于另一种获取订阅密钥的方法是使用 Management API call

使用az rest可以:

APIMID=`az apim show -n apimname -g resourcegroup --query id -o tsv`
az rest --method post  --uri ${APIMID}/subscriptions/test-subscription/listSecrets?api-version=2021-08-01 --query primaryKey -o tsv

其中 test-subscription 是订阅的名称。