共用方式為


about_PSCustomObject

簡短描述

說明和 [pscustomobject] 型別加速器之間的差異[psobject]

詳細描述

類型 [pscustomobject] 加速器已在PowerShell 3.0中新增。

新增此類型加速器之前,使用成員屬性和值建立對象會比較複雜。 最初,您必須使用 New-Object 來建立 物件,以及 Add-Member 新增屬性。 例如:

PS> $object1 = New-Object -TypeName PSObject
PS> Add-Member -InputObject $object1 -MemberType NoteProperty -Name one -Value 1
PS> Add-Member -InputObject $object1 -MemberType NoteProperty -Name two -Value 2
PS> $object1 | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
one         NoteProperty int one=1
two         NoteProperty int two=2

PS> $object1

one two
--- ---
  1   2

之後,您可以使用 的 Property 參數來傳遞包含成員和值的 Hashtable。 New-Object 例如:

PS> $object2 = New-Object -TypeName PSObject -Property @{one=1; two=2}
PS> $object2 | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
one         NoteProperty int one=1
two         NoteProperty int two=2

PS> $object2

one two
--- ---
  1   2

因為 PowerShell 3.0,所以轉換 哈希表[pscustomobject] 達到相同的結果。

PS> $object3 = [pscustomobject]@{one=1; two=2}
PS> $object3 | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
one         NoteProperty int one=1
two         NoteProperty int two=2

PS> $object3

one two
--- ---
  1   2

PSObject 類型物件會依成員加入對象的順序維護成員清單。 即使 Hashtable 物件不保證索引鍵/值組的順序,仍會轉換常值哈希表來 [pscustomobject] 維護順序。

哈希表必須是常值。 如果您將哈希表包裝在括弧中,或如果您轉換包含哈希表的變數,則不保證會保留順序。

$hash = @{
    Name      = "Server30"
    System    = "Server Core"
    PSVersion = "4.0"
}
$Asset = [pscustomobject]$hash
$Asset
System      Name     PSVersion
------      ----     ---------
Server Core Server30 4.0

瞭解類型加速器

[psobject][pscustomobject] 是類型加速器。

如需詳細資訊,請參閱 about_Type_Accelerators

即使您認為應該 [pscustomobject] 對應至 System.Management.Automation.PSCustomObject,但類型不同。

PS> [pscustomobject] -eq [System.Management.Automation.PSCustomObject]
False

這兩種類型加速器都會對應至相同的類別 PSObject

PS> [pscustomobject]

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

PS> [psobject]

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

[pscustomobject]類型加速器新增至 PowerShell 時,它包含額外的程式代碼來處理哈希表轉換為 PSObject 類型的轉換。 只有在建立新的物件時,才會叫用這個額外的程序代碼。 因此,您無法用於 [pscustomobject] 類型強制或類型比較,因為所有物件都會被視為 PSObject 類型。

例如,使用 -is 運算符來檢查 Cmdlet 傳回的物件是否與 [pscustomobject] 比較 [psobject]相同。

PS> (Get-Item /) -is [pscustomobject]
True

PS> (Get-Item /) -is [psobject]
True

當您將任何物件 [psobject] 轉換成您取得原始物件的類型時。 因此,將哈希表[pscustomobject]以外的任何項目轉換成產生相同類型。

PS> ([psobject]@{Property = 'Value'}).GetType().FullName
System.Collections.Hashtable

PS> ([pscustomobject]123).GetType().Name
Int32

PS> ([pscustomobject]@{Property = 'Value'}).GetType().FullName
System.Management.Automation.PSCustomObject

雖然,將 對象轉型為[psobject]似乎對類型沒有任何影響,但 PowerShell 會在 物件周圍新增不可見[psobject]的包裝函式。 這可能會有微妙的副作用。

  • 包裝的物件符合其原始型別和型別 [psobject]

    PS> 1 -is [Int32]
    True
    PS> 1 -is [psobject]
    False
    PS> ([psobject] 1) -is [Int32]
    True
    PS> ([psobject] 1) -is [psobject]
    True
    
  • 格式運算子 (-f) 無法辨識由 包裝的 [psobject]陣列。

    PS> '{0} {1}' -f (1, 2)
    1 2
    PS> '{0} {1}' -f ([psobject] (1, 2))
    Error formatting a string: Index (zero based) must be greater than or equal
    to zero and less than the size of the argument list..
    

轉換包含類似索引鍵的哈希表

區分大小寫的字典可能包含只區分大小寫的索引鍵名稱。 當您將這類字典 [pscustomobject]轉換成 時,PowerShell 會保留索引鍵的案例,但不會區分大小寫。 因此:

  • 第一個重複索引鍵的案例會成為該索引鍵的名稱。
  • 最後一個 case-variant 索引鍵的值會變成 屬性值。

下列範例示範此行為:

$Json = '{
  "One": 1,
  "two": 2,
  "Two": 3,
  "three": 3,
  "Three": 4,
  "THREE": 5
}'
$OrderedHashTable = $Json | ConvertFrom-Json -AsHashTable
$OrderedHashTable

請注意,已排序的哈希表包含多個索引鍵,只依大小寫而不同。

Name                           Value
----                           -----
One                            1
two                            2
Two                            3
three                          3
Three                          4
THREE                          5

當該哈希表轉換成 時,會使用第一個 [pscustomobject]索引鍵的名稱大小寫,但會使用最後一個相符索引鍵名稱的值。

[PSCustomObject]$OrderedHashTable
One two three
--- --- -----
  1   3     5

備註

在 Windows PowerShell 中,透過轉換 Hashtable [pscustomobject] 建立的對象沒有 LengthCount 屬性。 嘗試存取這些成員會傳 $null回 。

例如:

PS> $object = [PSCustomObject]@{key = 'value'}
PS> $object

key
---
value

PS> $object.Count
PS> $object.Length

從 PowerShell 6 開始,藉由將 Hashtable 轉換成永遠具有 Length 和 Count 屬性的值1所建立的物件。 [pscustomobject]

另請參閱