2014-02-21 43 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]' –