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. (type powershell.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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -