簡體   English   中英

如何從Powershell v3中的哈希數組生成PSCustomObjects數組?

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

假設我在Powershell v3中有一系列哈希:

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

我可以將單個哈希轉換為PSCustomObject如下所示:

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

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

> $co.GetType()

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

到現在為止還挺好。 當我嘗試將整個哈希數組轉換為管道中的PSCustomObjects ,會出現問題:

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

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

如你所見,我得到一個Hashtable對象數組,而不是PSCustomObjects 為什么我會得到不同的行為,我怎樣才能實現我想要的目標?

謝謝。

而不是強制轉換,請嘗試直接調用New-Object

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

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

奇怪的是,這似乎有效:

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

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

為什么? 我不知道,使用流水線哈希表條目執行轉換似乎有困難。

我還沒弄清楚確切的原因(每個人還在學習一些東西),但你的問題與$_ pipe元素有關。 如果我強制演示$_我可以讓你的代碼工作

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

產量

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

好奇的東西

我不喜歡NameValue ,而我正在測試(這就是文字哈希表對標題的影響而我在測試時發現它令人困惑),所以我將后者更改為Data然后輸出不同。 我只是因為好奇而發布它。 在帖子中很難顯示結果。

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

這似乎有效:

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

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

似乎管道變量$_值被包裝到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