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

标签 powershell

我在文件夹中有与 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

我只是想将此过滤器传递给变量中的 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!>>

如果我不将过滤器保存在变量中,它就可以正常工作:

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

但我需要在脚本中的变量中传递过滤器。

有什么想法吗?

最佳答案

尝试这样:

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

-include 参数接受[string[]](string 类型的数组)

关于Powershell:如何将过滤器值传递给gci,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434952/

相关文章:

powershell - 禁用-TlsCipherSuite : Exception from HRESULT: 0xD0000225

powershell - 如何检查程序是否已安装,如果未安装,如何安装?

powershell - 通过Visual Studio使用CMake运行Powershell脚本,而无需更改系统范围的权限

c# - 在 C# 中创建 PowerShell Cmdlet - 管道链

powershell - 在其路径中引用包含方括号 ([]) 的文件

powershell - 如何使用powershell为azure ad中的应用程序授予权限

r - 连接断开时如何在PowerShell中对R脚本自动重试

Powershell 无法正确读取负数

PowerShell 使用来自程序集 "Could not load type ' 的 netcoreapp3.0 程序集 : Add-Type fails with 'System.Private.CoreLib"System. Object'

powershell - 模块范围的变量在没有显式赋值的情况下被更改