如何使用PowerShell在Azure订阅之间列出资源组? [英] How to list Resource Groups across Azure Subscriptions using PowerShell?

查看:35
本文介绍了如何使用PowerShell在Azure订阅之间列出资源组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写PowerShell脚本,以列出Azure订阅中的资源组.

I'm writing a PowerShell script to list Resource Groups across Azure Subscriptions.

Get-AzureRmSubscription | 
select -ExpandProperty name |
 % { 
     Get-AzureRmResourceGroup | select -ExpandProperty resourcegroupname
 }

此代码有效.它返回这样的结果.

This code works. It returns results like this.

Resource group 1     
Resource group 2
Resource group 3 

如何调整代码以获取如下所示的输出?

How can I tweak the code to get the output like below?

Subscription 1,Resource group 1     
Subscription 1,Resource group 2
Subscription 2,Resource group 3 

非常感谢.

推荐答案

未经测试:

Get-AzureRmSubscription | 
 % { 
   $subscrName = $_.Name
   Select-AzureSubscription -Current -SubscriptionName $name
   (Get-AzureRmResourceGroup).resourcegroupname | % {     
     [pscustomobject] @{
       Subscription = $subscrName
       ResourceGroup = $_
     }
   }
 }

注意:以上内容更改了会话的当前订阅.在您的真实代码中,您可能之后希望恢复以前的最新代码.

上面的代码输出具有 .Subscription .ResourceGroup 属性的自定义对象;如果您真的只想输出字符串,请使用:
"$ name,$ _"

The above outputs custom objects with .Subscription and .ResourceGroup properties; if you really only want to output strings, use:
"$name,$_"

这篇关于如何使用PowerShell在Azure订阅之间列出资源组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆