关于select-string中path参数通过管道获取的问题


PowerShell交流中心分类: Questions关于select-string中path参数通过管道获取的问题
0
snakepoon asked 8 年 ago

在查看select-string的帮助是被告知-path是接受管道并且是 bypropertyname的:

-Path <string[]>

是否必需?                    True
位置?                        1
是否接受管道输入?            True (ByPropertyName)
参数集名称          File
别名                     无
动态?                    false

但是若然新建一个object并且添加string数组到-path noteproperty中,再通过管道传至select-string中,只会被当做-inputobject来处理:

$test = New-Object psobject | select Path

$stringarray = @(“c:\”,”f:\a.txt”)

$test.path = $stringarray

此时的test变量:

PS F:\> $test.path
c:\
f:\a.txt

PS F:\> $test | gm

TypeName:Selected.System.Management.Automation.PSCustomObject

Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Path        NoteProperty string[] Path=System.String[]

再然后就是从管道传递给select-string:

$test | Select-String -pattern “txt”

按道理能够把f:\a.txt的内容搜出来,可是行不通。然而如果我传入的是fileinfo的对象反而是可以的:

PS F:\> dir f:\a.txt|select-string -Pattern “txt”

a.txt:1:txt

请问能看出来我的通过管道传递包含属性path的对象给select-string的语句有那里不正确吗?谢谢

1 Answers
0
Mooser Lee 管理员 answered 8 年 ago

这个-Path指的是文件路径,不是随便一个Object中的Path属性。 怎么理解-Path是文件路径,比如: PS> Select-String -Path .\test.ps1 ‘U’ 怎么理解-Path接受管道输入:   比如: PS> dir *.ps1 | Select-String ‘U’