未显示所有属性 [英] Not all properties displayed

查看:35
本文介绍了未显示所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们尝试通过管道将数据导出到其他函数时,我们在 PowerShell 中观察到一些奇怪的行为.

When we're trying to export data to other functions via the pipeline, we observe some strange behavior in PowerShell.

示例代码:

$Array = @()

$Obj1 = [PSCustomObject]@{
    Member1   = 'First'
    Member2   = 'Second'
}

$Obj2 = [PSCustomObject]@{
    Member1   = 'First'
    Member2   = 'Second'
    Member3   = 'Third'
}

$Array = $Obj1, $Obj2
$Array | Out-GridView -Title 'Not showing Member3'

$Array = $Obj2, $Obj1
$Array | Out-GridView -Title 'All members correctly displayed'

在上面的例子中你可以看到当第一个对象只包含 2 个 properties 时,Out-GridView CmdLet(和其他)只显示 2 个 properties,即使第二个对象有 3 个 属性.但是,当数组中的第一个对象具有 3 个 properties 时,它确实会正确显示它们.

In the example above you can see that when the first object only contains 2 properties, the Out-GridView CmdLet (and others) only show 2 properties, even though the second object has 3 properties. However, when the first object in the array has 3 properties it does display them all correctly.

有没有办法解决这个问题?因为无法预先预测对象上将有多少 properties 以及具有最多 properties 的对象是否将是 数组中的第一个.

Is there a way around this? Because it's not possible to predict up front how many properties on an object there will be and if the object with the most properties will be the first one in the array.

推荐答案

我曾经有过同样的经历,并创建了以下可重用的Union"函数:

I had the same experience once and created the following reusable 'Union' function:

# 2021-08-25 Removed Union function

用法:

$Obj1, $Obj2 | Union | Out-GridView -Title 'Showing all members'

它还应该处理复杂的对象.一些标准 cmdlet 一次输出多种对象类型,如果您查看它们(例如 Out-GridView)或将它们转储到文件中(例如 Export-Csv),您可能会错过很多属性.再举个例子:

It is also supposed to work with complex objects. Some standard cmdlets output multiple object types at once and if you view them (e.g. Out-GridView) or dump them in a file (e.g. Export-Csv) you might miss a lot of properties. Take as another example:

Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class hp_biosSetting | Union | Export-Csv ".\HPBIOS.csv"


于 2014 年 9 月 19 日添加:

也许这已经在注释的两行之间 $Array |选择 * |... 不会解决问题,但会专门选择属性 $Array |选择会员 1、会员 2、会员 3 |... 确实如此.
此外,虽然在大多数情况下 Union 函数可以工作,但也有一些例外,因为它只会将 first 对象与其余对象对齐.考虑以下对象:

Maybe this is already between the lines in the comments $Array | Select * | … will not resolve the issue but specifically selecting the properties $Array | Select Member1, Member2, Member3 | … does.
Besides, although in most cases the Union function will work, there are some exceptions to that as it will only align the first object with the rest. Consider the following object:

$List = @(
    New-Object PSObject -Property @{Id = 2}
    New-Object PSObject -Property @{Id = 1}
    New-Object PSObject -Property @{Id = 3; Name = "Test"}
)

如果你 Union 这个对象一切似乎都很好,如果你例如ExportTo-CSV 并使用 export .csv 文件从那时起您将永远不会遇到任何问题.

If you Union this object everything appears to be fine and if you e.g. ExportTo-CSV and work with the export .csv file from then on you will never have any issue.

$List | Union
Id Name
-- ----
 2
 1
 3 Test

仍然有一个问题,因为只有第一个对象对齐.如果你例如按 Id (Sort Id) 对结果进行排序,或仅取最后 2 个 (Select -Last 2) 条目,Name 未列出,因为第二个对象不包含 Name 属性:

Still there is a catch as only the first object is aligned. If you e.g. sort the result on Id (Sort Id) or take just the last 2 (Select -Last 2) entries, the Name is not listed because the second object doesn’t contain the Name property:

$List | Union | Sort Id
Id
--
 1
 2
 3

为此我重写了Union-Object(别名Union)函数`):

Therefor I have rewritten the Union-Object (Alias Union) function`):

# 2021-08-25 Removed Union-Object function

语法:

$Array | Union | Out-GridView -Title 'All members correctly displayed'


更新 2021-08-25

基于 az1d 对相同属性名称不同大小写导致错误的有用反馈, 我创建了一个新的 UnifyProperties 函数.
(我将不再为他使用名称UnionObject)

function UnifyProperties {
  $Names = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
  $InputCollected = @($Input)
  $InputCollected.ForEach({ 
    foreach ($Name in $_.psobject.Properties.Name) { $Null = $Names.Add($Name) }
  })
  $inputCollected | Select-Object @($Names)
}

用法:

[pscustomobject] @{ one = 1; two = 2; three = 3 },
[pscustomobject] @{ ONE = 10; THREE = 30; FOUR = 4 } |
    UnifyProperties

one two three FOUR
--- --- ----- ----
  1   2     3
 10        30 4

另见:#13906 将 -UnifyProperties 参数添加到 Select-对象

See also: #13906 Add -UnifyProperties parameter to Select-Object

这篇关于未显示所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆