简体   繁体   English

Powershell:如何将过滤器值传递给gci

[英]Powershell: how to pass filter value to gci

I've got files that matches M*,S* in a folder: 我在一个文件夹中有匹配M *,S *的文件:

PS E:\Uploader> dir M*,S*
    Directory: E:\Uploader
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-----         6/05/2013  10:43 AM    5046659 M_1813O.zip
-a---        26/04/2013   4:51 PM        233 SurveyUploader.config
-a---         8/05/2013   5:15 PM      11788 SurveyUploader.log
-a---         8/05/2013   5:15 PM       6078 SurveyUploader.ps1
-----         6/05/2013  10:43 AM    3473113 S_1813O.zip

I simply want to pass this filter to gci in a variable, but it doesn't work: 我只是想将此过滤器传递给变量中的gci,但是它不起作用:

PS E:\Uploader> $k="M*,S*"
PS E:\Uploader> gci *.* -include $k | where {!$_.PsIsContainer}
PS E:\Uploader> gci *.* -include @($k) | where {!$_.PsIsContainer}
<<no files returned!>>

If I don't save the filter in a variable, it works fine: 如果我不将过滤器保存在变量中,则可以正常工作:

PS E:\Uploader> gci *.* -include S*,M* | where {!$_.PsIsContainer}
    Directory: E:\temp\SurveyLoadingAutomation\Uploader
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-----         6/05/2013  10:43 AM    5046659 M_1813O.zip
-a---        26/04/2013   4:51 PM        233 SurveyUploader.config
-a---         8/05/2013   5:15 PM      11788 SurveyUploader.log
-a---         8/05/2013   5:15 PM       6078 SurveyUploader.ps1
-----         6/05/2013  10:43 AM    3473113 S_1813O.zip

But I need to pass the filter in a variable in a script. 但是我需要在脚本的变量中传递过滤器。

Any idea? 任何想法?

Try like this: 尝试这样:

$k=  @("M*","S*")

the -include parameter accept [string[]] (an array of string type ) -include参数接受[string[]]string类型的数组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM