【问题标题】:converting system.array object into json object in powershell在powershell中将system.array对象转换为json对象
【发布时间】:2020-01-21 15:50:01
【问题描述】:

我正在使用 azure cli 登录 Azure,如下所示:

$login = az login

输出如下:

[
  {
    "cloudName": "AzureCloud",
    "id": "xxxxxxxxxxxxxxxxxxx",
    "isDefault": false,
    "name": "ABC",
    "state": "Enabled",
    "tenantId": "yyyyyyyyyyyyy",
    "user": {
      "name": "xxxxxxx",
      "type": "user"
    }
  },
  {
    "cloudName": "AzureCloud",
    "id": "xxxxxxxxxxxxxxxxxxxx",
    "isDefault": true,
    "name": "PQR",
    "state": "Enabled",
    "tenantId": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
    "user": {
      "name": "xxxxxxxxxx",
      "type": "user"
    }
  }
]

我想获取名称为 PQR 的 id 值。 我期待我可以这样做:

$login[1].id

但事实证明 $login 不是 json 对象。我试着做 $login |转换为 JSON。没有按我的意愿工作。

【问题讨论】:

  • 你的意思是ConvertFrom-Json?字符串已经是 JSON,你想要一个对象。
  • @Jeroen Mostert 甚至 ConvertFrom-Json 都没有给我预期的输出
  • 你能分享一下$login.GetType()是什么吗?
  • @stud3nt $login.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True对象[] System.Array

标签: powershell


【解决方案1】:

az login 在这种情况下只会给你一个输出行数组。您可以使用$login.GetType() 进行检查,这将为您提供System.Object[]。您可以使用 ConvertFrom-Json 将数组转换为 PowerShell 自定义对象。然后您可以简单地访问第二个对象的订阅id 属性。

$login = az login

$json = $login | ConvertFrom-Json

Write-Output $json[1].id
# ID should be printed here

您可以使用Get-Member 查看 PSCustomObject 属性:

PS C:\> $json | Get-Member

    TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition                                                                                
----        ----------   ----------                                                                                
Equals      Method       bool Equals(System.Object obj)                                                            
GetHashCode Method       int GetHashCode()                                                                         
GetType     Method       type GetType()                                                                            
ToString    Method       string ToString()                                                                         
cloudName   NoteProperty string cloudName=AzureCloud                                                               
id          NoteProperty string id=xxxx-xxxx-xxxx-xxxx                                        
isDefault   NoteProperty bool isDefault=True                                                                       
name        NoteProperty string name=xxxxxxx                                                 
state       NoteProperty string state=Enabled                                                                      
tenantId    NoteProperty string tenantId=xxxx-xxxx-xxxx-xxxx                               
user        NoteProperty System.Management.Automation.PSCustomObject user=@{name=xxxx@something.com; type=user}

您还可以查看 Connect-AzAccount,这是 Azure PowerShell 的登录方式。完整的教程可以在 Sign in with Azure PowerShell 找到。 az login 是 Azure CLI 登录方式,跨平台兼容。

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 2020-12-14
    • 2017-06-16
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多