简体   繁体   English

如何从Powershell v3中的哈希数组生成PSCustomObjects数组?

[英]How can I generate an array of PSCustomObjects from an array of hashes in Powershell v3?

Suppose I have an array of hashes in Powershell v3: 假设我在Powershell v3中有一系列哈希:

> $hashes = 1..5 | foreach { @{Name="Item $_"; Value=$_}}

I can convert a single hash into a PSCustomObject like this: 我可以将单个哈希转换为PSCustomObject如下所示:

> $co = [PSCustomObject] $hashes[0]
> $co | ft -AutoSize

Value Name  
----- ----  
    1 Item 1

> $co.GetType()

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

So far, so good. 到现在为止还挺好。 The problem occurs when I try to convert the entire hash array into PSCustomObjects in a pipeline: 当我尝试将整个哈希数组转换为管道中的PSCustomObjects ,会出现问题:

> ($hashes | foreach { [PSCustomObject] $_})[0].getType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     -------- 
True     True     Hashtable                                System.Object

As you see, I'm getting an array of Hashtable objects, not PSCustomObjects . 如你所见,我得到一个Hashtable对象数组,而不是PSCustomObjects Why do I get different behavior, and how can I accomplish what I want? 为什么我会得到不同的行为,我怎样才能实现我想要的目标?

Thanks. 谢谢。

Instead of casting, try calling New-Object directly: 而不是强制转换,请尝试直接调用New-Object

# > ($hashes | foreach { New-Object -TypeName PSCustomObject -Property $_})[0].getType()

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

Also, oddly, this seems to work: 奇怪的是,这似乎有效:

# > (0..4 |  foreach { ([PSCustomObject]$hashes[$_])})[0].GetType()

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

Why? 为什么? I have no idea, it seems to have trouble performing the cast using a pipelined hashtable entry. 我不知道,使用流水线哈希表条目执行转换似乎有困难。

I have yet to figure out the exact cause (Everyone is still learning something) but your issue is related to the $_ pipe element somehow. 我还没弄清楚确切的原因(每个人还在学习一些东西),但你的问题与$_ pipe元素有关。 I can make your code work if I force a cast of the $_ 如果我强制演示$_我可以让你的代码工作

$hashes = 1..5 | foreach { @{Name="Item $_"; Value=$_}}
$hashes | %{([pscustomobject][hashtable]$_)}

Output 产量

Value Name  
----- ----  
    1 Item 1
    2 Item 2
    3 Item 3
    4 Item 4
    5 Item 5

Something curious 好奇的东西

I didn't like Name and Value , while I was testing (That is what a literal hash table had for headers and I found it confusing while I was testing), so I changed the later to Data and then the output differs. 我不喜欢NameValue ,而我正在测试(这就是文字哈希表对标题的影响而我在测试时发现它令人困惑),所以我将后者更改为Data然后输出不同。 I only post it as it is curious. 我只是因为好奇而发布它。 It is hard to show the results in the post. 在帖子中很难显示结果。

Name                                                                                                                                                     Data
----                                                                                                                                                     ----
Item 1                                                                                                                                                      1
Item 2                                                                                                                                                      2
Item 3                                                                                                                                                      3
Item 4                                                                                                                                                      4
Item 5                                                                                                                                                      5

This seems to work: 这似乎有效:

$hashes = 1..5 | foreach { @{Name="Item $_"; Value=$_}}

foreach ($hash in $hashes)
{([PSCustomObject]$hash).gettype()}

It seems that value of pipeline variable $_ get wrapped into PSObject and that break cast to PSCustomObject . 似乎管道变量$_值被包装到PSObject并且断开PSObject转换为PSCustomObject

> $hashes=@{Value=1},[PSObject]@{Value=2}

> $hashes[0].GetType().FullName
System.Collections.Hashtable
> $hashes[1].GetType().FullName
System.Collections.Hashtable
# It seems that both $hashes elements are Hashtable,

> [Type]::GetTypeArray($hashes).FullName
System.Collections.Hashtable
System.Management.Automation.PSObject
# But, as you can see, second one is not.

> ([PSCustomObject]$hashes[0]).GetType().FullName
System.Management.Automation.PSCustomObject
> ([PSCustomObject]$hashes[1]).GetType().FullName
System.Collections.Hashtable
# And that difference break cast to PSCustomObject.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM