Powershell从对象中选择Note属性类型对象

rn0zuynd  于 2023-03-18  发布在  Shell
关注(0)|答案(3)|浏览(191)

我正在使用深度嵌套的JSON,在convertfrom-json之后,需要能够遍历convertfrom-json cmdlet生成的对象的各个部分。
我没有办法提前知道哪些属性名可能在对象内部,也可能不在对象内部,据我所知,有数百种不同的可能属性。幸运的是,我看到的一件有帮助的事情是,我关心的每个属性都是类型“NoteProperty”。
下面是一个例子:

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()
definition         NoteProperty System.Management.Automation.PSCustomObject definition=@{$schema=https://schema.management.azure.com/providers/Microsof... 
integrationAccount NoteProperty System.Management.Automation.PSCustomObject integrationAccount=@{id=[parameters('integrationAccounts_xxx_integration... 
parameters         NoteProperty System.Management.Automation.PSCustomObject parameters=@{$connections=}
state              NoteProperty string state=Enabled

因此,我认为创建一个函数会很简单,该函数将只选择当前正在处理的级别的对象,这些对象为“MemberType”和“NoteProperty”。
我已尝试将对象通过管道传输到:

where-object { $_.MemberType -eq "NoteProperty" }

没有。
我也尝试过各种形式的select-object,但似乎不能选择我需要的东西。我从脚本编写人员那里找到一篇关于使用标签和表达式的老文章-但这似乎有点矫枉过正,不是吗?有人能告诉我选择NoteProperty项的简单方法吗?
谢谢!

kyks70gy

kyks70gy1#

您可以使用隐藏的.psobject.properties来迭代成员。

$json = @'
{
  "users": [
    {
      "userId": 1,
      "firstName": "Krish",
      "lastName": "Lee",
      "phoneNumber": "123456",
      "emailAddress": "krish.lee@learningcontainer.com"
    },
    {
      "userId": 2,
      "firstName": "racks",
      "lastName": "jacson",
      "phoneNumber": "123456",
      "emailAddress": "racks.jacson@learningcontainer.com"
    }
  ]
}
'@ | ConvertFrom-Json

$json | foreach {
    $_.psobject.properties | foreach {
        Write-Host Property Name: $_.name
        Write-Host Values: $_.value
    }
}

你可以根据需要继续。

$json | foreach {
    $_.psobject.properties | foreach {
        $_.value | foreach {
            $_.psobject.properties | foreach {
                write-host Property name: $_.name
                write-host Property value: $_.value
            }
        }
    }
}

Property name: userId
Property value: 1
Property name: firstName
Property value: Krish
Property name: lastName
Property value: Lee
Property name: phoneNumber
Property value: 123456
Property name: emailAddress
Property value: krish.lee@learningcontainer.com
Property name: userId
Property value: 2
Property name: firstName
Property value: racks
Property name: lastName
Property value: jacson
Property name: phoneNumber
Property value: 123456
Property name: emailAddress
Property value: racks.jacson@learningcontainer.com
kkbh8khc

kkbh8khc2#

要使用 * 广义 * 解决方案补充Doug Maurer's helpful answer
下面的代码段定义并调用函数Get-LeafProperty,该函数递归遍历对象图-例如ConvertFrom-Json返回的对象图-并**输出所有 leaf 属性值,沿着它们在层次结构中的 name path*。

# Define a walker function for object graphs:
# Get all leaf properties in a given object's hierarchy,
# namely properties of primitive and quasi-primitive types 
# (.NET primitive types, plus those that serialize to JSON as a single value).
# Output:
#  A flat collection of [pscustomobject] instances with .NamePath and .Value 
#  properties; e.g.:
#   [pscustomobject] @{ NamePath = 'results.users[0].userId'; Value = 1 }
function Get-LeafProperty {
  param([Parameter(ValueFromPipeline)] [object] $InputObject, [string] $NamePath)
  process {   
    if ($null -eq $InputObject -or $InputObject -is [DbNull] -or $InputObject.GetType().IsPrimitive -or $InputObject.GetType() -in [string], [datetime], [datetimeoffset], [decimal], [bigint]) {
      # A null-like value or a primitive / quasi-primitive type -> output.
      # Note: Returning a 2-element ValueTuple would result in better performance, both time- and space-wise:
      #      [ValueTuple]::Create($NamePath, $InputObject)
      [pscustomobject] @{ NamePath = $NamePath; Value = $InputObject }
    }
    elseif ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [System.Collections.IDictionary]) {
      # A collection of sorts (other than a string or dictionary (hash table)), 
      # recurse on its elements.
      $i = 0
      foreach ($o in $InputObject) { Get-LeafProperty $o ($NamePath + '[' + $i++ + ']') }
    }
    else { 
      # A non-quasi-primitive scalar object or a dictionary:
      # enumerate its properties / entries.
      $props = if ($InputObject -is [System.Collections.IDictionary]) { $InputObject.GetEnumerator() } else { $InputObject.psobject.properties }
      $sep = '.' * ($NamePath -ne '')
      foreach ($p in $props) {
        Get-LeafProperty $p.Value ($NamePath + $sep + $p.Name)
      }
    }
  }
}

使用示例:

# Parse sample JSON with multiple hierarchy levels into a [pscustomobject]
# graph using ConvertFrom-Json.
$objectGraphFromJson = @'
{
  "results": {
      "users": [
          {
              "userId": 1,
              "emailAddress": "jane.doe@example.com",
              "attributes": {
                  "height": 165,
                  "weight": 60
              }
          },
          {
              "userId": 2,
              "emailAddress": "john.doe@example.com",
              "attributes": {
                  "height": 180,
                  "weight": 72
              }
          }
      ]
  }
}
'@ | ConvertFrom-Json

# Get all leaf properties.
Get-LeafProperty $objectGraphFromJson

以上结果为:

NamePath                                          Value
--------                                          -----
results.users[0].userId                               1
results.users[0].emailAddress      jane.doe@example.com
results.users[0].attributes.height                  165
results.users[0].attributes.weight                   60
results.users[1].userId                               2
results.users[1].emailAddress      john.doe@example.com
results.users[1].attributes.height                  180
results.users[1].attributes.weight                   72
kq0g1dla

kq0g1dla3#

另一种方法是将结果放入一些选择表达式中,然后按此排序。
在您的示例中,您将integrationAccount和parameters作为注解属性,因此,如果它们位于名为$results的pscustomobject中

$results = $results | Select @{l="integrationAccount ";e={$_.integrationAccount }},@{l="parameters ";e={$_.parameters }}

然后你就可以在上面做一个选择和/或Where-Object。

相关问题