excel - 访问未知级别的 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
}
}
变量的类型$findings
是PSCustomObject
PS C:\Users\bryanar> $findings.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
有什么建议吗?
解决方案
假设我有一个$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。它非常冗长,但非常适合需要重用对象时,因为它保留了类型信息。
推荐阅读
- node.js - 为什么我的控制台日志记录:`TimeoutOverflowWarning:4294967296000 不适合 32 位有符号整数`
- youtube-dl - youtube-dl 是否仍然有效(最新版本 youtube-dl-2020.2.16)?
- javascript - 读取流不执行触发/捕获错误
- regex - 如何在正则表达式中允许一个空格
- reactjs - 如何允许 scss 开玩笑地对 typescript nextjs 进行单元测试?
- python - 处理不同大小的 TF 输入
- python-telegram-bot - bot.get_me() 不起作用并引发错误
- reactjs - 如果会话 ReactJS 重定向
- reactjs - React App 未使用 express-socket.io-session 在浏览器中设置 cookie
- sql - After truncate , we can not rollback data in SQL?