간단한 설명
[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 매개 변수를 사용하여 멤버와 값이 포함된 Hashtable 전달할 수 있습니다. 예를 들어:
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] 형식으로 처리되므로 형식 강제 변환 또는 형식 비교에 사용할 수 없습니다.
예를 들어 -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]형식과 일치합니다.PS> 1 -is [int32] True PS> 1 -is [psobject] False PS> ([psobject] 1) -is [int32] True PS> ([psobject] 1) -is [psobject] Trueformat 연산자(
-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에서 [pscustomobject] 캐스팅하여 만든 개체에는 Length 또는 Count 속성이 없습니다.
이러한 멤버에 액세스하려고 시도하면 $null반환됩니다.
예를 들어:
PS> $object = [pscustomobject]@{key = 'value'}
PS> $object
key
---
value
PS> $object.Count
PS> $object.Length
PowerShell 6부터 [pscustomobject] 캐스팅하여 만든 개체에는 항상 1 및 Count 속성에 대한 값이 있습니다.
참고하십시오
- about_Object_Creation
- about_Objects
- System.Management.Automation.PSObject
- System.Management.Automation.PSCustomObject
PowerShell