需要更快地在PowerShell脚本中进行搜索

时间:2016-11-27 06:36:27

标签: search fastercsv acceleration select-string boost-foreach

谁能帮助您更快地进行搜索?使用此代码搜索需要几天时间。 Search_Names.csv(约10k名称) Need_This_Long_Strings.csv(大约180k字符串,它是50MB)

$TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
$SearchNames = gc D:\Search_Names.csv
$WhereSearch = gc D:\Need_This_Long_Strings.csv
$Val = 0

foreach ($SearchName in $SearchNames)
{
       $WhereSearch | Where{$_ | Select-String -Pattern "$SearchName.*"} | Out-File D:\Find_in_Search_File_$TimeStamp.log -Append
       $Val = $Val + 1
}
"Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。 刚刚将数据(Need_This_Long_Strings.csv)转换为数组。 现在这个代码搜索需要大约20分钟。

$TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
$SearchNames = Get-Content D:\Search_Names.csv
$WhereSearch = Import-Csv D:\Need_This_Long_Strings.csv -Delimiter ";"
$SearchArray = New-Object System.Collections.ArrayList($null)
$SearchArray.AddRange($WhereSearch)
$Val = 0

foreach ($_ in $SearchArray)
{
       if ($SearchNames -contains $_.FullName)
       {
             $_ | Export-Csv D:\Find_in_Search_File_$TimeStamp.csv -Delimiter ";" -Append
             $Val = $Val + 1
       }
}
"Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append