2014-02-21 1 views
5

저는 powershell의 초보자이며 간단한 문제가 무엇인지 의심합니다. 다음 명령을 수행하려고하지만 결과적으로 아무것도 반환하지 않으며 이유를 이해할 수 없습니다.powershell select-string이 올바르게 작동하지 않습니다.

bcdedit의 현재 섹션에 대한 설명을 얻으려고합니다. 내가 할 경우

bcdedit /enum | select-string "identifier.*current" -context 0,3 

그것은 다음 반환

> identifier {current} 
device partition=C: 
path \WINDOWS\system32\winload.exe 
description Windows 8.1 

왜하지 않습니다 다음 반환 description Windows 8.1?

bcdedit /enum | select-string "identifier.*current" -context 0,3 | select-string "description" 

대신 아무것도 반환하지 않습니다.

이 정보에 감사드립니다.

답변

7

Select-String은 문자열을 출력하지 않으므로 MatchInfo 개체이므로 예상 한 결과를 얻지 못합니다.

 
PS C:\>bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | Get-Member 

    TypeName: Microsoft.PowerShell.Commands.MatchInfo 

Name   MemberType Definition 
----   ---------- ---------- 
Equals  Method  bool Equals(System.Object obj) 
GetHashCode Method  int GetHashCode() 
GetType  Method  type GetType() 
RelativePath Method  string RelativePath(string directory) 
ToString  Method  string ToString(), string ToString(string directory) 
Context  Property Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;} 
Filename  Property string Filename {get;} 
IgnoreCase Property bool IgnoreCase {get;set;} 
Line   Property string Line {get;set;} 
LineNumber Property int LineNumber {get;set;} 
Matches  Property System.Text.RegularExpressions.Match[] Matches {get;set;} 
Path   Property string Path {get;set;} 
Pattern  Property string Pattern {get;set;} 

PS C:\>bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | Format-List * 

IgnoreCase : True 
LineNumber : 17 
Line  : identifier    {current} 
Filename : InputStream 
Path  : InputStream 
Pattern : identifier.*current 
Context : Microsoft.PowerShell.Commands.MatchInfoContext 
Matches : {identifier    {current} 

Line 속성은 실제 일치하는 라인을 포함하고 Context 속성은 사전에 자식 속성을 포함 : 파이프 Get-Member 또는 Format-List cmdlet을에 첫 번째 Select-String의 출력이 경우,이 같은 뭔가를 얻을 수 있습니다 - 및 사후 문맥. 당신이 찾고있는 description 라인이 PostContext 자식 속성이기 때문에, 당신은 그 라인을 추출하기위한이 같은 뭔가가 필요 :

bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | 
    Select-Object -Expand Context | 
    Select-Object -Expand PostContext | 
    Select-String 'description' 

결론 : Select-String이 제대로 작동 않습니다. 그것은 당신이 기대하는대로 작동하지 않습니다.

+2

또는 약간 간단하고 다시 검색을 선택 문자열 몇 가지 문자열을주고 -stream''아웃 문자열을 사용할 수'BCDEDIT/열거 | select-string "identifier. * current"- 텍스트 0,3 | Out-String - 스트림 | Select-String "description"' –

2

Select-String은 표시되는 문자열 데이터뿐만 아니라 MatchInfo 개체를 반환합니다. 이 데이터는 MatchInfo 개체의 LineContext 속성에서 가져옵니다.

bcdedit /enum | select-string "identifier.*current" -context 0,3 | format-list 

을 그리고 당신은 MatchInfo 개체의 다양한 속성을 볼 수 있습니다 :

이보십시오. Context 속성은 당신이 얻을 더이 개체를 드릴 다운 더 많은 정보가 필요합니다 Microsoft.PowerShell.Commands.MatchInfoContext로 표시되는

참고 :이

(bcdedit /enum | select-string "identifier.*current" -context 0,3).context | format-list 

는 것을 볼 수 있습니다을 context 속성은 PreContextPostContext 속성이있는 또 다른 객체로, 실제 Pre 및 PostContext 행이 있습니다.

그래서 :

(bcdedit /enum | select-string "identifier.*current" -context 0,3).Context.PostContext | Select-String 'description' 

postcontext 경기에서 설명 라인을 얻을 것이다.

또는 당신이 할 수 있습니다 :

[string](bcdedit /enum | select-string "identifier.*current" -context 0,3) -split "`n" -match 'description' 
+0

'(bcdedit/enum | Select-String "식별자. * current"- 컨텍스트 3) .Context.PostContext [2]' –