about_PSCustomObject
간단한 설명
형식 가속기와 [psobject]
형식 가속기의 차이점을 [pscustomobject]
설명합니다.
자세한 설명
[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
나중에 속성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 참조하세요.
System.Management.Automation.PSCustomObject[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 형식으로 변환하는 것을 처리하는 추가 코드가 포함되었습니다. 이 추가 코드는 새 개체를 만들 때만 호출됩니다.
따라서 모든 개체가 PSObject[pscustomobject]
처리되므로 형식 강제 변환 또는 형식 비교에 사용할 수 없습니다.
예를 들어 연산자를 -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은 해당 키의 대/소문자를 유지하지만 대/소문자를 구분하지는 않습니다. 결과적으로 다음이 수행됩니다.
- 첫 번째 중복 키의 경우는 해당 키의 이름이 됩니다.
- 마지막 대/소문자 변형 키의 값은 속성 값이 됩니다.
다음 예제에서는 이 동작을 보여 줍니다.
$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을 캐스팅하여 만든 개체에는 Length[pscustomobject]
없습니다.
이러한 멤버에 액세스하려고 시도하면 반환됩니다 $null
.
예시:
PS> $object = [PSCustomObject]@{key = 'value'}
PS> $object
key
---
value
PS> $object.Count
PS> $object.Length
PowerShell 6부터 Hashtable을 캐스팅하여 만든 개체는 항상 Length[pscustomobject]
1
값을 갖습니다.
참고 항목
PowerShell