powershell脚本消耗所有内存

时间:2015-03-03 20:45:48

标签: regex powershell

我正在运行以下脚本来检查一组文件中的卡号。当我对一组总共600mb的38个文件进行运行时,它会消耗max cpu(限制50%)和max memory(3.3GB 4.0GB物理)。

寻找关于为什么会这样做以及如何优化这一点的想法。

谢谢!

Get-ChildItem "c:\REGEX\ScanMeFiles\" -Recurse |`
Foreach-Object{
    $content = Get-Content $_.FullName
    $outfile = 'c:\regex\results\'+$_.BaseName+'_results.log'
 $content | Where-Object {$_ -match '\b(?:3[47]\d|(?:4\d|5[1-5]|65)\d{2}|6011)\d{12}\b'}  | Set-Content $outfile
}

2 个答案:

答案 0 :(得分:0)

我会让它更加包容。用更少的变量做这样的事情:

$children = (Get-ChildItem).FullName
foreach($child in $children){
    Get-Content $child | ?{$_ -match '\b(?:3[47]\d|(?:4\d|5[1-5]|65)\d{2}|6011)\d{12}\b'} | Set-Content ('c:\regex\results\'+$_.BaseName+'_results.log')
}

答案 1 :(得分:0)

在Matt的帮助下,这就是我想出的。对我的测试数据运行<1分钟。谢谢!

Get-ChildItem "c:\REGEX\ScanMeFiles\" |
Foreach-Object{
    $content = $_.FullName
    $outfile = 'c:\regex\results\'+$_.BaseName+'_results.log'
    $regex = '\b(?:3[47]\d|(?:4\d|5[1-5]|65)\d{2}|6011)\d{12}\b'
select-string -Path $content -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } | Set-Content $outfile