2011-10-03 5 views
44

변수 $a$b의 차이점은 무엇입니까? GetType이 PowerShell에서 사용됨, 변수 간 차이

$a = (Get-Date).DayOfWeek 
$b = Get-Date | Select-Object DayOfWeek 

나는
$a.GetType 
$b.GetType 

MemberType   : Method 
OverloadDefinitions : {type GetType()} 
TypeNameOfValue  : System.Management.Automation.PSMethod 
Value    : type GetType() 
Name    : GetType 
IsInstance   : True 

MemberType   : Method 
OverloadDefinitions : {type GetType()} 
TypeNameOfValue  : System.Management.Automation.PSMethod 
Value    : type GetType() 
Name    : GetType 
IsInstance   : True 

을 확인하려하지만, 이러한 변수의 출력이 다른 보이지만 차이 없을 것 같습니다.

답변

87

첫째, 당신은 GetType을 호출하는 괄호 부족하다. 당신이 보는 것은 [DayOfWeek]에서 GetType 메소드를 설명하는 MethodInfo입니다. 실제로 GetType을 호출하려면 어떻게해야 :

$a.GetType(); 
$b.GetType(); 

당신은 $a은 [된 요일] 것을 볼해야하고, $b는 데이터 객체 만 된 요일 속성을 캡처 할 Select-Object cmdlet에 의해 생성 된 사용자 정의 개체입니다. 따라서, 그것은 단지 된 요일 속성을 가진 객체는 다음과 같습니다

C:\> $b.DayOfWeek -eq $a 
True 
11

Select-Object는 새 psobject를 만들고 요청한 속성을 복사합니다. 당신은 GetType을()으로이를 확인할 수 있습니다 : 모든

PS > $a.GetType().fullname 
System.DayOfWeek 

PS > $b.GetType().fullname 
System.Management.Automation.PSCustomObject 
6

Select-Object 지정된 단지 속성 정의 PSObject을 반환합니다. 하나의 속성으로도 ACTUAL 변수를 얻지 못합니다. 그것은 PSObject 안에 싸여있다. 대신

는 수행

Get-Date | Select-Object -ExpandProperty DayOfWeek 

당신에게 동일한 결과를 얻을 것이다으로 :

(Get-Date).DayOfWeek 

의 차이는 Get-Date 반환 여러 개체 경우, 파이프 라인의 방법은 괄호 방법보다 더 나은 작품이다 예를 들어, (Get-ChildItem)은 항목의 배열입니다. 이것은 PowerShell v3에서 변경되었으며 (Get-ChildItem).FullPath은 예상대로 작동하고 전체 경로의 배열을 반환합니다.