문제

여기에 뭔가가 빠졌습니다.

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher  
$objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry  
$objSearcher.Filter = ("(objectclass=computer)")  
$computers = $objSearcher.findall()  

그렇다면 질문은 왜 다음 두 출력이 다른가 하는 것입니다.

$computers | %{ 
"Server name in quotes $_.properties.name" 
"Server name not in quotes " + $_.properties.name 
}
PS> $computers[0] | %{"$_.properties.name"; $_.properties.name}
System.DirectoryServices.SearchResult.properties.name
GORILLA
도움이 되었습니까?

해결책

$_.properties.name을 문자열에 포함시키면 속성의 유형 이름이 반환됩니다.변수가 문자열에 포함되어 있고 문자열이 평가되면 변수에서 참조하는 해당 개체에 대해 ToString 메서드를 호출합니다(뒤에 지정된 멤버는 포함되지 않음).

이 경우 ToString 메서드는 유형 이름.EBGreen이 제안한 것과 유사하게 변수 및 멤버를 강제로 평가할 수 있지만 다음을 사용하여

"Server name in quotes $($_.properties.name)"  

다른 시나리오에서는 파워셸 먼저 지정된 변수와 멤버를 평가한 다음 이를 이전 문자열에 추가합니다.

속성 모음을 돌려받는 것이 맞습니다.파이프를 하면 $컴퓨터[0].속성 get-member를 사용하려면 명령줄에서 바로 개체 모델을 탐색할 수 있습니다.

중요한 부분은 아래와 같습니다.

유형이름:System.DirectoryServices.ResultPropertyCollection

이름 회원 유형 정의


값 속성 System.Collections.ICollection 값 {get;}

다른 팁

나는 PS가 ""에 정보를 삽입하는 방식과 관련이 있다고 생각합니다.이 시도:

"따옴표로 묶인 서버 이름 $($_.properties).name"

아니면 $() 세트가 하나 더 필요할 수도 있습니다.지금은 테스트할 수 있는 곳이 없습니다.

닫기-- 아래 내용은 올바르게 작동하지만 더 자세한 설명이 있는 사람이 있으면 관심이 있을 것입니다.

PS C:\> $computers[0] | %{ "$_.properties.name"; "$($_.properties.name)" }
System.DirectoryServices.SearchResult.properties.name
GORILLA

따라서 $_.properties.name은 내가 예상한 대로 따르지 않는 것 같습니다.올바르게 시각화하는 경우 name 속성이 다중값이라는 사실로 인해 배열이 반환됩니다.(제 생각에는) 다음이 작동하는 이유를 설명합니다.

$computers[0] | %{ $_.properties.name[0]}

"name"이 문자열인 경우 첫 번째 문자를 반환해야 하지만 배열이기 때문에 첫 번째 문자열을 반환합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top