Select-Object는 사용자 지정 개체의 배열을 파이프 라인에 반환합니다. 이 경우에는 하나의 속성 만 문자열이됩니다.
@walidtourni에 언급 된 바와 같이 expand
을 사용하면이 문제를 해결할 수 있습니다. expand
은 해당 값을 가진 속성이있는 사용자 지정 개체 대신 출력이 속성의 값이되기 때문입니다. 이것이 가능한 이유는 expand
은 하나의 인수 만 취합니다. 즉, 동일한 "행"에 대해 여러 값을 반환하려고 시도 할 가능성이 없습니다.
반면에 foreach-object는 단순히 파이프 라인에 물건을 뱉어내는 것입니다. 사용자 지정 개체에 둘 다 수동으로 래핑하지 않고 두 번째 속성을 포함하려고하면 동일한 행에 두 개의 속성 대신 다른 행이 만들어집니다.
는
Clear
$x = Get-Service | Where-Object {$_.Status -eq "Running"} | select -first 3
$x | foreach-object {$_.Name} #returns 3 rows, single column; string
$x | foreach-object {$_.Name,$_.CanPauseAndContinue;} #returns 6 rows, single column; alternate string & boolean
$x | foreach-object {$_.Name;$_.CanPauseAndContinue;} #returns 6 rows, single column; alternate string & boolean
$x | select Name #returns 3 rows, single column (string); custom object
$x | select Name, CanPauseAndContinue #returns 3 rows, two columns (string & boolean); custom property
$x | select -expand Name #returns 3 rows, single column; string; notice the lack of column header showing this is a string, not a string property of a custom object
$x | select -expand Name,CanPauseAndContinue #error; -expand can only take single valued arguments
$x | select -expand Name -expand CanPauseAndContinue #error; you can't try to define the same paramenter twice
설명에 감사드립니다. 정말 도움이되었습니다. –
@SubhayanBhattacharya 감사의 말을 나타내는 가장 좋은 방법은 John의 답을 정확하다고 표시하는 것입니다. – Matt