首页 > 解决方案 > 访问未知级别的 PSCustomObject 嵌套对象

问题描述

我从具有未知嵌套级别属性的 API 收到响应,这是一个示例:

affects_rating          : True
assets                  : {@{asset=xxxxxxxxxxxxx; identifier=; category=low; importance=0.0; is_ip=True}}
details                 : @{check_pass=; diligence_annotations=; geo_ip_location=NL; grade=GOOD; remediations=System.Object[]; vulnerabilities=System.Object[]; dest_port=443; rollup_end_date=2021-06-06; 
                          rollup_start_date=2020-03-18}
evidence_key            : xxxxxxxxx:xxxx
first_seen              : 2020-03-18
last_seen               : 2021-06-06
related_findings        : {}
risk_category           : Diligence
risk_vector             : open_ports
risk_vector_label       : Open Ports
rolledup_observation_id : xxxx-xxxx==
severity                : 1.0
severity_category       : minor
tags                    : {}
asset_overrides         : {}
duration                : 
comments                : 
remaining_decay         : 59

temporary_id            : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
affects_rating          : True
assets                  : {@{asset=xxxx.xxxx.com; identifier=; category=low; importance=0.0002340946; is_ip=False}, @{asset=xxxx.xxxx.com; identifier=; category=critical; importance=0.45131093; is_ip=False},

到目前为止,我尝试使用表访问每个值,但有时记录包含一个对象,该对象输出System.Object[]到 CSV 文件。

foreach ($item in $findings.results) { 
    $tabledata = [ordered]@{
        temporary_id     = $item.temporary_id
        affects_rating   = $item.affects_rating
        asset            = $item.assets.asset
        asset_identifier = $item.assets.identifier
        asset_category   = $item.assets.category
        asset_importance = $item.assets.importance
        asset_is_ip      = $item.assets.is_ip
        modal_data       = $item.details.diligence_annotations.modal_data
        modal_tags       = $item.details.diligence_annotations.modal_tags
        server           = $item.details.diligence_annotations.server
    }
}

变量的类型$findingsPSCustomObject

PS C:\Users\bryanar> $findings.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                   
-------- -------- ----                                     --------                                                                                                                                                   
True     False    PSCustomObject                           System.Object 

有什么建议吗?

标签: excelpowershellnestedpscustomobject

解决方案


假设我有一个$result充满属性、列表或其他对象的对象:

$result = [pscustomobject]@{Name='Foo';Level='1'
  Object= [pscustomobject]@{Name='Bar';Level='2'
    List= @(
      [pscustomobject]@{User='John Smith';Dept='Accounting'},
      [pscustomobject]@{User='Bob Smith';Dept='Accounting'}
)}}
$result | fl

Name   : Foo
Level  : 1
Object : @{Name=Bar; Level=2; List=System.Object[]}

如果您只想查看整个对象以进行故障排除/探索,我发现最简单的方法是将其转换为 json 或 xml:

$result | ConvertTo-Json -Depth 10
{
  "Name":  "Foo",
  "Level":  "1",
  "Object":  {
    "Name":  "Bar",
    "Level":  "2",
    "List":  [
      {
        "User":  "John Smith",
        "Dept":  "Accounting"
      },
      {
        "User":  "Bob Smith",
        "Dept":  "Accounting"
      }
    ]
  }
}

如果要保存这样的对象,请使用Export-CLIXML而不是 CSV。它非常冗长,但非常适合需要重用对象时,因为它保留了类型信息。


推荐阅读