简体   繁体   English

Powershell“select-string”阅读信息

[英]Powershell “select-string” Read Info

EngConI need help getting info out of the Select-String command. EngConI需要帮助从Select-String命令中获取信息。 I need to find which items from $array are inside the engcon.pbo file. 我需要找到来自$array哪些项目在engcon.pbo文件中。 It would be most useful if the found results were displayed on the console or even better, in a .txt file. 如果找到的结果显示在控制台上,甚至更好,在.txt文件中,这将是最有用的。

In the full code, there are 297 items in the array (#0 and #295 included). 在完整代码中,数组中有297个项目(包括#0和#295)。

######
#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. 如果可能的话,你也可以提供一些学习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. 在使用“write-host”进行快速测试之后,结果显示foreach ($item in $array)某些内容foreach ($item in $array)导致错误(立即结束脚本),我使用的示例文件只是一些数组项的测试者和一些随机的单词,全部用空格分隔。 As for the code, all I edited was the set of items in $array 至于代码,我编辑的只是$ 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) “理解随机Outspeed你好你不可思议的Marsileaceae极端Runcation Guggle Tribunitious Chalybeate”是所有版本的完整测试者文件(EngCon.pbo,EngCon.txt和EngCon)

Your Select-String instruction is repeatedly reading $TargetFile . 您的Select-String指令重复读取$TargetFile This will adversely affect performance. 这将对性能产生不利影响。 Try something like this instead: 尝试这样的事情:

$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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM