如何正确地过滤PowerShell复制脚本中的多个string

我从这个答案使用PowerShell脚本做文件复制。 当我想要使用filter包含多个文件types时出现问题。

Get-ChildItem $originalPath -filter "*.htm" | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item $_.FullName -destination $targetFile } 

像梦一样工作。 但是,当我想要使用filter包含多个文件types时出现问题。

 Get-ChildItem $originalPath ` -filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*") | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item $_.FullName -destination $targetFile } 

给我以下错误:

 Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported. At F:\data\foo\CGM.ps1:121 char:36 + Get-ChildItem $originalPath -filter <<<< "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*" | ` + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand 

我有各种括号的迭代,没有括号, -filter-include ,定义包含variables(例如, $fileFilter ),每次获得上述错误,并始终指向任何$fileFilter -filter

有趣的例外是当我编码-filter "*.gif,*.jpg,*.xls*,*.doc*,*.pdf*,*.wav*,*.ppt*" 。 没有错误,但我没有得到任何结果,没有任何东西回到控制台。 我怀疑我是不经意地编了一个不正当的声明呢?

那么我做错了什么,我该如何纠正呢?

-Filter只接受一个string。 -Include接受多个值,但限定了-Path参数。 诀窍是将\*附加到path的末尾,然后使用-Includeselect多个扩展。 顺便说一句,在cmdlet参数中引用string是不必要的,除非它们包含空格或shell特殊字符。

 Get-ChildItem $originalPath\* -Include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt* 

请注意,无论$ originalPath是否以反斜杠结尾,这都会起作用,因为多个连续的反斜杠被解释为单个path分隔符。 例如,尝试:

 Get-ChildItem C:\\\\\Windows 

这样的事情应该工作(它为我做的)。 想要使用-Filter而不是-Include -Filter是包含了比-Filter占用巨大的性能。

下面只是循环每个文件types和多个服务器/工作站在不同的文件中指定。

 ## ## This script will pull from a list of workstations in a text file and search for the specified string ## Change the file path below to where your list of target workstations reside ## Change the file path below to where your list of filetypes reside $filetypes = gc 'pathToListOffiletypes.txt' $servers = gc 'pathToListOfWorkstations.txt' ##Set the scope of the variable so it has visibility set-variable -Name searchString -Scope 0 $searchString = 'whatYouAreSearchingFor' foreach ($server in $servers) { foreach ($filetype in $filetypes) { ## below creates the search path. This could be further improved to exclude the windows directory $serverString = "\\"+$server+"\c$\Program Files" ## Display the server being queried write-host “Server:” $server "searching for " $filetype in $serverString Get-ChildItem -Path $serverString -Recurse -Filter *.txt | #-Include "*.xml","*.ps1","*.cnf","*.odf","*.conf","*.bat","*.cfg","*.ini","*.config","*.info","*.nfo","*.txt" | Select-String -pattern 'cifs01' | group path | select name | out-file f:\DataCentre\String_Results.txt $os = gwmi win32_operatingsystem -computer $server $sp = $os | % {$_.servicepackmajorversion} $a = $os | % {$_.caption} ## Below will list again the server name as well as its OS and SP ## Because the script may not be monitored, this helps confirm the machine has been successfully scanned write-host $server “has completed its " $filetype "scan:” “|” “OS:” $a “SP:” “|” $sp } } #end script 
 Get-ChildItem $originalPath\* -Include @("*.gif", "*.jpg", "*.xls*", "*.doc*", "*.pdf*", "*.wav*", "*.ppt")