2014-02-21 44 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對象。如果您的管道第一Select-String輸出到Get-MemberFormat-List cmdlet的,你會得到這樣的事情:

 
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屬性包含與預先子屬性 - 和後上下文。由於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 /枚舉|選擇字符串「標識符。*當前」-Context 0,3 | Out-String -Stream | 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「標識符。*當前」-Context 3).Context.PostContext [2]' –