并非所有属性都显示 [英] Not all properties displayed

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

问题描述

当我们尝试通过管道将数据导出到其他函数时,我们在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时,即使第二个对象具有3个propertiesOut-GridView CmdLet(和其他对象)也仅显示2个properties.但是,当数组中的第一个对象具有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最多的对象将是array中的第一个对象,则是不可能的.

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:

Function Union {
    $Union = @()
    $Input | ForEach {
        If ($Union.Count) {$_ | Get-Member | Where {!($Union[0] | Get-Member $_.Name)} | ForEach {$Union[0] | Add-Member NoteProperty $_.Name $Null}}
        $Union += $_
    }
    $Union
}

用法:

$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-09-19:

也许这已经在注释中$Array | Select * | …的两行之间了,但不能解决问题,但是专门选择属性$Array | Select Member1, Member2, Member3 | …可以.
此外,尽管在大多数情况下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`):

Function Union-Object ([String[]]$Property = @()) {         # Version 00.02.01, by iRon
    $Objects = $Input | ForEach {$_}
    If (!$Property) {ForEach ($Object in $Objects) {$Property += $Object.PSObject.Properties | Select -Expand Name}}
    $Objects | Select ([String[]]($Property | Select -Unique))
} Set-Alias Union Union-Object

语法:

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

有关最新的Union-Object版本,请参见: https://powersnippets.com/union -object/

For the latest Union-Object version, see: https://powersnippets.com/union-object/

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

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