Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将多个值传递给参数_Powershell - Fatal编程技术网

Powershell 将多个值传递给参数

Powershell 将多个值传递给参数,powershell,Powershell,我想将字符串传递给变量$FileExtension,如果我传递一个值,如$FileExtension=“*.txt”,则它工作正常,但如果传递多个值,如$FileExtension=“*.txt,*.log”,则它失败 如何解决这个问题 Get-ChildItem *.* -Include $FileExtension | Select-String -pattern "$SearchString" | group path | select name | Out-File $OutputLo

我想将字符串传递给变量
$FileExtension
,如果我传递一个值,如
$FileExtension=“*.txt”
,则它工作正常,但如果传递多个值,如
$FileExtension=“*.txt,*.log”
,则它失败

如何解决这个问题

Get-ChildItem *.* -Include $FileExtension | Select-String -pattern "$SearchString" | group path | 
select name | Out-File $OutputLocation'\'$OutputFile

-Include
参数采用
字符串[]
,因此您可以简单地创建如下字符串数组:

$FileExtension = "*.txt","*.log" 
$fileextension=".ini",".jpg"
Get-ChildItem "c:\temp" -file | where Extension -in $fileextension
Get-ChildItem "*.*" -file -Include $fileextension | 
    Select-String -pattern "$SearchString" -List | 
        select Path  | 
            Out-File $OutputLocation'\'$OutputFile

@MartinBrandl的解决方案对于性能来说更好,因为它直接进入getchilditem命令。但使用此解决方案,get childitem命令的路径文件中必须有“*”。注意:您可以在GetChildItem中使用-file选项以获得更好的性能

另一种解决方案是对过滤器扩展使用where命令,如下所示:

$FileExtension = "*.txt","*.log" 
$fileextension=".ini",".jpg"
Get-ChildItem "c:\temp" -file | where Extension -in $fileextension
Get-ChildItem "*.*" -file -Include $fileextension | 
    Select-String -pattern "$SearchString" -List | 
        select Path  | 
            Out-File $OutputLocation'\'$OutputFile
可以在select string命令中使用-lisrt,然后可以按如下方式删除group命令:

$FileExtension = "*.txt","*.log" 
$fileextension=".ini",".jpg"
Get-ChildItem "c:\temp" -file | where Extension -in $fileextension
Get-ChildItem "*.*" -file -Include $fileextension | 
    Select-String -pattern "$SearchString" -List | 
        select Path  | 
            Out-File $OutputLocation'\'$OutputFile

如果我想搜索子目录,我如何在您的代码中应用?