PSHELL: How pass arguments to script?

May 20, 2008
11,411
99
Syracuse, NY, USA
Here's a little Powershell script.

Code:
# cmenu.ps1
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace($args[0])
$file = $folder.ParseName($args[1])
$file.InvokeVerb("Properties")

It works (opens the Properties dialog) in Powershell if I give it a foldername and a filename; for example,

{code]PS> cmenu.psi d:\tc26 tcc.exe[/code]

If, instead of arg[0] and arg[1], I hard-code a foldername and a file name, it runs OK with TCC's PSHELL.

But I can't figure out how call it with PSHELL providing two arguments to the script.
 
If I do;
Code:
pshell /s cmenu.ps1 e:\utils cmenu.ps1
it works, but gives an error.

Not sure why it works with /s, but not without.

I also made a slight change to your script;
Code:
# cmenu.ps1
param($p1, $p2)
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace($p1)
$file = $folder.ParseName($p2)
$folder
$file
$file.InvokeVerb("Properties")

Not a game changer, just the method that I use for getting arguments.

Joe
 
Hard-coding the foldername and filename, as you noted, works with PSHELL.

I've written a .BTM that creates the .PS1, thus allowing for arguments that are hard-coded into the .PS1
Code:
@setlocal
@echo off
if %# ne 2 quit
type <<- endtext > cmenu.ps1
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace("%1")
$file = $folder.ParseName("%2")
$file.InvokeVerb("Properties")
endtext
type cmenu.ps1
pshell cmenu.ps1
endlocal

1595422750587.png


Not a fix, but an alternative.

Joe
 
May 20, 2008
11,411
99
Syracuse, NY, USA
Indeed, this works (i.e., opens the Properties dialog).

Code:
pshell /s v:\cmenu.ps1 d:\tc26 tcc.exe

And generates this error:

1595430234834.png
 
May 20, 2008
11,411
99
Syracuse, NY, USA
You can also pass the info in the environment. That makes an alias feasible. But passing args to the script would be better.

Code:
alias psprops `set p1=%@path[%1] & set p2=%@filename[%1] & pshell v:\cmenue.ps1 & unset p1 p2`

Code:
v:\> type cmenue.ps1
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace($env:p1)
$file = $folder.ParseName($env:p2)
$file.InvokeVerb("Properties")
 
May 20, 2008
11,411
99
Syracuse, NY, USA
Throw in another variable for the verb and it gets pretty generic.

Code:
v:\> alias verbex
set p1=%@path[%1] & set p2=%@filename[%1] & set p3=%2 & pshell v:\doverb.ps1

v:\> type doverb.ps1
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace($env:p1)
$file = $folder.ParseName($env:p2)
$file.InvokeVerb($env:p3)
 
May 20, 2008
11,411
99
Syracuse, NY, USA
This works and produces no error (might have gleaned it from the help).

Code:
pshell /s "v:\cmenu.ps1 d:\tc26 tcc.exe"
 
May 20, 2008
11,411
99
Syracuse, NY, USA
Here's another incarnation.

Code:
param($verb, $target)
$path = split-path -path $target -parent
$filename = split-path -path $target -leaf
$o = new-object -com "Shell.Application"
$folder = $o.NameSpace($path)
$file = $folder.ParseName($filename)
$file.InvokeVerb($verb)

Call it with the verb first and a regular drive:\path\file.ext file spec second.

Code:
pshell /s ".\cmenu.ps1 properties d:\tc26\tcc.exe"
 
  • Like
Reactions: Joe Caverly

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
FWIW - I have an executable extension for .ps1.
Code:
set .ps1=powershell -f
I am able to execute your last example by
Code:
cmenu Properties C:\tc26\tcc.exe
 
May 20, 2008
11,411
99
Syracuse, NY, USA
FWIW - I have an executable extension for .ps1.
Code:
set .ps1=powershell -f
I am able to execute your last example by
Code:
cmenu Properties C:\tc26\tcc.exe
That's funny. I can't. "Open" works, but "Properties" gives just nothing.
 
May 20, 2008
11,411
99
Syracuse, NY, USA
When I try to use an executable extension (as Scott suggested) and the "Properties" verb it does nothing and I get an event log entry

1595465353885.png


With the verb "Open", it's OK ... it works and no Scriptblock event log entry. Can I turn this off?
 
May 20, 2008
11,411
99
Syracuse, NY, USA
That's funny. I can't. "Open" works, but "Properties" gives just nothing.
Apparently powershell.exe was exiting too quickly for the "Properties" verb to work with the executable extension. Putting "Sleep -s 1" at the end of the PS! script solved that. I wonder if it's possible to force InvokeVerb to be (more) synchronous.
 
May 20, 2008
11,411
99
Syracuse, NY, USA
Apparently powershell.exe was exiting too quickly for the "Properties" verb to work with the executable extension. Putting "Sleep -s 1" at the end of the PS! script solved that. I wonder if it's possible to force InvokeVerb to be (more) synchronous.
That gets the "Properties" dialog to show, but it closes when the transient Powershell exits. Doesn't that happen to you, Scott?
 
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly PSHELL and COM Object in v27 Support 1
Joe Caverly PSHELL in v27 vs. 26 Support 17
vefatica @PSHELL? Support 5
Peter Murschall v27.00.17: PSHELL /S dosn't do nothing Support 8
Joe Caverly PSHELL and NTFS File Streams Support 2
Joe Caverly Using @PSHELL from @EVAL Support 8
vefatica PSHELL and @PSHELL fail the first time in a new TCC. Support 3
Peter Murschall Documentation Question to %@PSHELL Support 4
Joe Caverly PSHELL and @PSHELL errorlevel values Support 4
Joe Caverly PSHELL and out-of-process servers Support 2
Joe Caverly PSHELL /C Support 7
vefatica What happens to PSHELL after 20 seconds? Support 9
Joe Caverly .PS1 script does not work with PSHELL Support 11
Joe Caverly How to use Powershell Profile with PSHELL Support 14
samintz PSHELL's CWD Support 2
Joe Caverly Missing PSHELL in HELP Support 0
Joe Caverly PSHELL Blank Line Display Support 2
vefatica PSHELL? Support 6
Joe Caverly PSHELL and Receive-Job Support 4
Joe Caverly PSHELL Error? Support 16
Joe Caverly Documentation @PSHELL Support 0
M Fixed PSHELL command returns error Support 2
D Can alias pass parameters to for command? Support 3
M Backquoted parameters used in GoSub don't pass string as a single parameter Support 2
D How to? Pass OpenHere Folder to CMD? Support 6
A Unable to pass batch file arguments unaltered Support 18
samintz How to? Pass a structure to @WINAPI Support 1
Mordachai How to pass URL to sendmail? Support 10
A How to? Pass output from ffind to another command in a BTM file Support 5
J How to pass a character like " " to a function Support 3
Emilio III Control characters pass through? Support 6
mfarah WAD Odd behaviour with command-line arguments on TCMD. Support 7
D WAD The /@ and // arguments to TCMD.EXE have no decernable effect. Support 11
D ZIPSFX arguments Support 1
greyfairer TCC Quotes needed for Arguments containing '='? Support 11
nikbackm FUNCTION with variable number of arguments Support 17
M Missing PERL command-line arguments Support 1
dcantor Hex arguments in @BREAD, @BPEEK, etc. Support 0
p.f.moore Quoting arguments to a user defined function Support 4

Similar threads