如果我使用 Powershell 知道资源组名称,如何获取订阅名称

How can I get the Subscription Name if i know the resource Group Name using Powershell

所以,我有资源组名称,我想以编程方式(使用 powershell)设置对传入资源组的订阅。 当我执行 Get-AzResourceGroup 时,我得到 ResourceId,其中包含订阅 ID 作为长字符串 /subscriptions/<subscription id here>/resourceGroups/resourcegroupname 的一部分。有没有办法从该字符串中提取订阅 ID?

您可以通过触发以下获取您的订阅ID。您还需要安装 azure cli。

az account list  --refresh --query "[?contains(name, 'YOUR_SUBSCRIPTION')].id"

您还可以拆分 powershell 的输出。

$myinput= "/subscriptions/xxxxxx-xxxxxx-xxxxxx-xxxxx-xxxxxx/resourceGroups/resourcegroupname".Split("/")

Write-Host $myinput[2]