0

EngConI need help getting info out of the Select-String command. I need to find which items from $array are inside the engcon.pbo file. It would be most useful if the found results were displayed on the console or even better, in a .txt file.

In the full code, there are 297 items in the array (#0 and #295 included).

######
#Mine#
######


$TargetFile   = "C:\PowershellScripts\EngCon.pbo"

$array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")

$i = 0

while ($i -le 295)
{

$SearchString = $array[$i]

Select-String $TargetFile -pattern $SearchString

$i = $i + 1

}

#######
#Yours#
#######

$array = @($array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")
$found = @{}
Get-Content "C:\PowershellScripts\EngCon.txt" | % {
  $line = $_
  foreach ($item in $array) {
    if ($line -match $item) { $found[$item] = $true }
  }
}

$found.Keys | Out-File "C:\PowershellScripts\results.txt"

If possible could you also provide some good places to learn PS.

After quick testing with "write-host" the results show something in the foreach ($item in $array) is causing the error(ends script instantly), also the sample file I use is just a tester of some array items and some random words, all separated by spaces. As for the code, all I edited was the set of items in $array

FYI, I cannot reveal most of the array items as they are private

"Comprehension random Outspeed hello yours Uncovenable Marsileaceae extreme Runcation Guggle Tribunitious Chalybeate" Is The Full Tester File For All Versions(EngCon.pbo, EngCon.txt And EngCon)

4

1 回答 1

2

你的Select-String指令是反复阅读$TargetFile。这将对性能产生不利影响。尝试这样的事情:

$array = @(...)

$found = @{}
Get-Content "C:\PowershellScripts\EngCon.pbo" | % {
  $line = $_
  foreach ($item in $array) {
    if ($line -match $item) { $found[$item] = $true }
  }
}

$found.Keys | Out-File "C:\results.txt"
于 2013-06-30T11:39:42.337 回答