powershell - Powershell:对象对象的格式表?
问题描述
我有一个 PSCustomObjects 的数组列表,我可以使用 Format-Table 输出它,并且看起来像预期的那样。
Name Property1 Property2 Property3
---- --------- --------- ---------
name1 value1 value2 value3
现在它发生了,PSCustomObject 的一个属性需要保存两个(或更多)值,所以我决定遵循面向对象的方法并将该属性也更改为 PSCustomObject,最终出现在包含对象的对象列表中,然后输出如下所示:
Name Property1 Property2 Property3
---- --------- --------- ---------
name1 value1 @{Sub1=valueX; Sub2=valueY} value3
我正在寻找的是 Format-Table 的某种递归输出,它扩展了里面的对象,看起来像这样:
Name Property1 Sub1 Sub2 Property3
---- --------- ---- ---- ---------
name1 value1 valueX valueY value3
这有可能吗?或者在构建该对象列表时我是否必须回退到“正常”列表?
谢谢你!
解决方案
您需要递归地做的就是发现叶子值的“路径”,然后为它们生成属性表达式选择器:
function Format-FlatTable {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[psobject]$InputObject
)
begin {
function Get-FlatSelector {
param(
[psobject]$Object,
[string]$Prefix
)
# Enumerate all properties
$object.psobject.Properties |ForEach-Object {
# Nested custom object, let's recurse
if($_.Value -is [System.Management.Automation.PSCustomObject]){
Get-FlatSelector $_.Value -Prefix $_.Name
}
else {
if($prefix){
# We're not at the root level anymore, construct a property expression table
@{Name="$prefix.$($_.Name)";Expression=[scriptblock]::Create("`$_.$prefix.$($_.Name)")}
}
else{
# Still at the root level, we can select value properties by name alone
$_.Name
}
}
}
}
}
process
{
# Use the first input object to determine how to flatten
$selectors = Get-FlatSelector $InputObject[0]
# Use `Format-Table` to only select flattened property values
$InputObject |Format-Table $selectors
}
}
这将产生一个像你想要的表格格式,但没有任何命名冲突:
[pscustomobject]@{
Name='Name1'
Property1 = 'value1'
Property2 = [pscustomobject]@{
Sub1 = 'valueX'
Sub2 = 'ValueY'
}
Property3 = 'value3'
} |Format-FlatTable
输出:
Name Property1 Property2.Sub1 Property2.Sub2 Property3
---- --------- -------------- -------------- ---------
Name1 value1 valueX ValueY value3
推荐阅读
- excel - 索引匹配公式显示错误结果
- arrays - 从我的地图函数中获取类型错误
- vue.js - 什么时候在 Vue 发射名称中使用冒号?
- registry - 为什么不建议在 Inno Setup 中使用 HKCR 注册表根键?
- reactjs - 使用 Bootstrap 4 在 React js 中渲染视图的问题
- reactjs - d3 / React / Hooks - 更新不会清理旧的矩形和组
- c# - 在 WPF MVVM 中绑定到 CheckBox Click 事件
- java - 洋葱架构中的 Kafka 消费者
- javascript - 在 Angular 9 中创建多个复选框对我不起作用
- c++ - 连接到命名管道