powershell - Right Click context - Search folder .txt & .log files -
still new powershell , have been creating automation scripts, checks , balances , can think of make our jobs easier day day. contract working on extremely tight when comes applications , forbids opensource\freeware applications. have been working on using powershell job.
here have far:
1. created hkcr\directory\shell\powershell (default) reg_sz search folder text & log files
2. created hkcr\directory\shell\powershell\command (default) reg_sz
c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe -file c:\temp\rightclicksearch.ps1 -noexit -command set-location -literalpath '%l'
i able right click launch , script opens , prompts me keyword search. problem is, can't seem figure out how pass location script properly.
rightclicksearch.ps1
(i know $path isn't set, before hardcoded , know have pass variable menu)
$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
there 2 problems call powershell.exe
:
- you can't specify both
-file
,-command
parameters. can specify 1 of them. - whichever 1 do specify, has last parameter in command. in example,
-noexit
,-command
parameters ignored. (typepowershell.exe -?
explanation.)
the news powershell scripts can accept arguments using param
keyword. declare parameter @ top of script:
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
you call command line this:
c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe -file c:\temp\rightclicksearch.ps1 -path '%l'
since $path
1 , parameter, don't have specify name:
c:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe -file c:\temp\rightclicksearch.ps1 '%l'
ironically, use -command
parameter in same way. difference script file not dot-sourced, won't matter in example gave.
Comments
Post a Comment