【问题标题】:Azure resourcesAzure 资源
【发布时间】:2020-01-25 19:59:23
【问题描述】:

我有两个关于 Azure 云的问题 -

  1. 有没有办法在 azure 中获取所有资源 ID 以及订阅标签?
  2. 有没有办法根据标签值获取订阅中的所有resourceId?

提前致谢

【问题讨论】:

    标签: azure azure-resource-manager


    【解决方案1】:

    要获取带有标签的 ResourceId,请使用下面的 cmdlet

    Get-AzResource|select ResourceId,Tags
    

    根据 cmdlet 下方的标记值获取 ResourceId

    Get-AzResource -TagValue "<Replace tag value here>" |select ResourceId
    

    【讨论】:

      【解决方案2】:

      根据您使用的是新的 AZ 模块还是旧的 AzureRM 模块,您有几个选择。

      建议使用新的 AZ 模块从 Powershell(跨平台和更新的 Powershell 版本)向 Azure 运行。更多信息请看这里: https://docs.microsoft.com/en-us/powershell/module/az.resources/get-azresource

      查找资源的多个选项,并获取标签、resourceId(和其他属性) - 并允许您基于标签进行过滤;通过标签的哈希表。如果您只想筛选和获取某些资源(而不是从 Azure 获取所有资源并随后进行筛选),这会更好。通过标签获取资源的示例:

      # Using hashtable 
      $resources = Get-AzResource -tag @{"costCenter"="201011";} 
      # No filter - get all resource s
      $resources = Get-AzResource;
      # If using AzureRM 
      $resources = Get-AzureRMResource -tag @{"costCenter"="201011";} 
      

      或使用 -tagName -tagValue

      # Using -TagName & TagValue 
      $resources = Get-AzResource -TagName "costCenter" -TagValue "201111"; 
      # If using AzureRM 
      $resources = Get-AzureRMResource -TagName "costCenter" -TagValue "201111"; 
      

      然后您的 $resources 变量中就有所有资源 - 因此您可以轻松选择 ResourceId 和/或标签(以及其他属性):

      $resources|Select ResourceId
      

      如果您想发现资源中某个对象的属性 - 试试这个:

      $resources[0]|Get-Member 
      

      【讨论】:

        猜你喜欢
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-05
        • 2014-05-21
        • 2020-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多