如何在PowerShell复制脚本中正确筛选多个字符串
我正在使用中的PowerShell脚本进行文件复制。当我想使用过滤器包含多个文件类型时,问题就出现了如何在PowerShell复制脚本中正确筛选多个字符串,powershell,filter,copy,powershell-2.0,Powershell,Filter,Copy,Powershell 2.0,我正在使用中的PowerShell脚本进行文件复制。当我想使用过滤器包含多个文件类型时,问题就出现了 Get-ChildItem $originalPath -filter "*.htm" | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item
Get-ChildItem $originalPath -filter "*.htm" | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
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 $originalPath -filter "*.htm" | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
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
Get-ChildItem:无法将“System.Object[]”转换为参数“Filter”所需的类型“System.String”。不支持指定的方法。
在F:\data\foo\CGM.ps1:121 char:36
+获取ChildItem$originalPath-filter使用include是根据
-过滤器只接受单个字符串-Include接受多个值,但限定-Path参数。诀窍是将\*
附加到路径的末尾,然后使用-Include选择多个扩展。顺便说一句,在cmdlet参数中不需要引用字符串,除非它们包含空格或shell特殊字符
Get-ChildItem $originalPath\* -Include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt*
请注意,无论$originalPath是否以反斜杠结尾,这都将起作用,因为多个连续的反斜杠被解释为单个路径分隔符。例如,尝试:
Get-ChildItem C:\\\\\Windows
像这样的东西应该行得通(对我来说就是这样)。想要使用-Filter
而不是-Include
的原因是,与-Filter
相比,Include的性能受到了巨大的影响
下面仅循环在单独文件中指定的每个文件类型和多个服务器/工作站
##
## 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 $filetype |
#-Include "*.xml","*.ps1","*.cnf","*.odf","*.conf","*.bat","*.cfg","*.ini","*.config","*.info","*.nfo","*.txt" |
Select-String -pattern $searchstring | 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
那没用(-filter-include*.file,*.types
-filter-include(*.file,*.types)
,-filter-include“*.file”、“*.types”
和-filter-include(“*.file”、“*.types”)
根据我的上述问题全部出错。删除-filter
参数,只包括-include
(相同的引号和括号的迭代)没有导致运行时错误,但目标目录中没有结果集。呜呜!这一\*
技巧刚刚解决了大约六个问题。太棒了&谢谢!请注意通配符(\*
)如果指定了-Recurse
,则不需要。由于某些原因,在搜索目录时此操作不起作用?添加-Recurse可将此操作扩展到子文件夹-Recurse
不适用于-Include
(关于遍历子目录)。欢迎使用Stack Overflow!此答案出现在低质量审阅队列中,可能是因为您没有解释内容。如果您确实解释了(在您的答案中),您更有可能获得更多的投票,提问者实际上学到了一些东西!此外,它重复了我之前发布的答案中的代码,只是它将扩展列表包含在数组表达式求值运算符(@()
)中,这是多余的,因为逗号分隔的列表本质上是作为数组计算的。这是非常正确的,在这里的5个类似问题中,没有人指出,虽然我们不能做-filter*.jpg,*.png
但一次完成-filter*.jpg可能比一次完成-filter*.png并加入结果要快,而不是一次完成-Include*。jpg,*.png“
。我有一个包含126k个文件和18k个文件夹的文件夹。我在每个文件夹中递归搜索一个文件和一个文件夹。使用-Filter需要5秒,使用-Include需要30秒。在10秒内执行-Filter两次比执行-Include-go快三倍。