右键单击上下文 - 搜索文件夹.txt& .log文件

时间:2016-05-28 16:31:15

标签: powershell search

仍然是PowerShell的新手,并且一直在创建自动化脚本,检查和平衡以及我能想到的任何使我们的工作日复一日的工作。我们正在处理的合同在应用程序和禁止开源\免费软件应用程序方面非常紧张。所以我一直在努力使用PowerShell来完成这项工作。

这是我到目前为止: 1.创建HKCR \ Directory \ Shell \ powershell(默认)Reg_sz搜索文件夹文本&日志文件
2.创建HKCR \ Directory \ Shell \ powershell \ command(默认)Reg_sz

C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -file c:\temp\RightClickSearch.ps1 -NoExit -Command Set-Location -LiteralPath '%L'

我能够右键单击启动并打开脚本并提示我输入要搜索的关键字。问题是,我似乎无法弄清楚如何正确地将位置传递给脚本。

RightClickSearch.ps1
(我知道$ path未设置,在硬编码之前,我知道我必须从菜单中传递一个变量)

$promt = (Read-Host -Prompt "Enter Search Keyword")
Get-ChildItem -Path $Path -Include *.txt, *.log -Recurse | Select-String -Pattern $promt | Format-table -AutoSize -Property LineNumber,Filename,Path
Pause

1 个答案:

答案 0 :(得分:1)

致电powershell.exe时出现两个问题:

  • 您无法同时指定-File-Command参数。您只能指定其中一个。
  • 无论您做什么指定,它都必须是命令中的最后一个参数。在您的示例中,-NoExit-Command参数将被忽略。 (输入powershell.exe -?作为解释。)

好消息是,PowerShell脚本本身可以使用param关键字接受参数。只需在脚本顶部声明参数:

param ($Path)
$promt = (Read-Host -Prompt "Enter Search Keyword")
Get-ChildItem -Path $Path -Include *.txt, *.log -Recurse | Select-String -Pattern $promt | Format-table -AutoSize -Property LineNumber,Filename,Path
Pause

您可以从命令行中调用它:

C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -File c:\temp\RightClickSearch.ps1 -Path '%L'

由于$Path是唯一的参数,您甚至不必指定其名称:

C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -File c:\temp\RightClickSearch.ps1 '%L'

具有讽刺意味的是,您可以以完全相同的方式使用-Command参数。唯一的区别是你的脚本文件不是点源的,但在你给出的例子中并不重要。

相关问题