【问题标题】:powershell convertfrom-json select-object differencespowershell convertfrom-json 选择对象差异
【发布时间】:2020-04-28 10:22:15
【问题描述】:

考虑以下 json 有效负载(只是“az group list”中的一个示例 sn-p):

[

    {
        "id": "/subscriptions/1f512sf9-112c-4a7a-a580-665afe4761f4/resourceGroups/dev-rg",
        "location": "northeurope",
        "managedBy": null,
        "name": "dev-rg",
        "properties": {
          "provisioningState": "Succeeded"
        },
        "tags": {
          "Application": "Integrations",
          "Department": "Development Team",
          "DeployedDate": "09/01/2020",
          "Environment": "DEV",
          "FundedBy": "INT",
          "InterfaceId": "IFUS_007.1",
          "Project": "INT"
        },
        "type": "Microsoft.Resources/resourceGroups"
      }
    ]

有人可以解释为什么下面的工作原理

PS C:\Windows\system32> az group list | convertfrom-json | select-object @{n='RSG';e={$_.name}}

RSG
---
{dev-rg}

为什么下面没有(返回空白)

PS C:\Windows\system32> az group list | convertfrom-json | select-object name

name
----

【问题讨论】:

  • 请提供有效的json。
  • 道歉 - 提供了有效的 json
  • 原因是ConvertFrom-Json cmdlet 无法访问整个对象。将第一两个阶段包装在 () 中,您将获得所需的内容。

标签: powershell azure-cli


【解决方案1】:

这是一个关于 ConvertFrom-Json 和管道的 Windows PowerShell 错误。

对命令() 进行分组,以便正确评估它们。

(az group list | ConvertFrom-Json) | Select-Object name

它已在 PowerShell Core 中修复。

【讨论】:

    【解决方案2】:

    除了@7cc's 答案外,您还可以使用 Azure PowerShell 中的 Get-AzResourceGroup Cmdlet,这将为您提供一个 PowerShell 对象以立即使用。这比使用az group list 将来自Azure CLI 的JSON 输出转换为使用ConvertFrom-JsonSystem.Management.Automation.PSCustomObject 更容易。

    要在Name 列中获取所有资源组:

    Get-AzResourceGroup | Select-Object -Property @{Name = "Name"; Expression = { $_.ResourceGroupName } }
    

    或者只保留默认的ResourceGroupName 列:

    Get-AzResourceGroup | Select-Object -Property ResourceGroupName
    

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 2018-03-18
      • 2015-08-09
      • 1970-01-01
      • 2021-06-30
      • 2015-03-20
      • 2015-05-25
      • 1970-01-01
      相关资源
      最近更新 更多