Helping with Alias

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
KozakMak
Senior Member
Senior Member
Posts: 480
Joined: 2021-05-24, 12:39 UTC
Location: UA

Helping with Alias

Post by *KozakMak »

Code: Select all

[em_alias_ps_adm]
cmd=*%$SystemX86%\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit
param=$A
Trying: *ps "D:\Program Files\Total Commander 64\Utils\Rar.exe" or any path with spaces - get error like "D:\Program"...
What I'm doing wrong?
OS: Win10 | TC: latest x64
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

2KozakMak
To begin with, there's no need to put this part "%$SystemX86%\WindowsPowerShell\v1.0\powershell.exe" the way you do it.
By default, on Windows 10 (which is stated in your signature on this board), PowerShell (Windows PowerShell 5.1, I may assume in your case) executable path is listed within the %path% (or $env:path in PowerShell syntax, if you want) variable.
Thus, "powershell.exe", or even simply "powershell" would be pretty enough, like this:

Code: Select all

[em_alias_ps_adm]
cmd=powershell -ExecutionPolicy Bypass -NoExit
param=$A
Moreover, from my experience, "-ExecutionPolicy Bypass -NoExit" is abundant, and could be dropped. No point in using it as well.
So we can reduce your command up to this:

Code: Select all

[em_alias_ps_adm]
cmd=powershell
param=$A
However, this beautiful command has no chance to work since neither PowerShell, nor TotalCommander is given an idea of what $A variable (in PowerShell syntax) is intended to mean.

Second, I can see no point in using a proxy command like your "em_alias_ps_adm" to fully utilize PowerShell.

Third, could you, please, provide an end purpose for using PowerShell with rar.exe. What exactly do you intend to do?

Still, here are two example commands that should work for you:

Code: Select all

[em_test_rar_commander_path]
cmd=powershell -c si -path env:path -value $env:path';'$env:commander_path\Utils;
param=rar.exe -?;pause

Code: Select all

[em_test_rar_full_path]
cmd=powershell -c si -path env:path -value $env:path';'D:\Program' 'Files\Total' 'Commander' '64\Utils;
param=rar.exe -?;pause
Some explanations:

Your path "D:\Program Files\Total Commander 64\Utils\rar.exe" implies rar.exe is residing within the TotalCommander tree, so it falls under the %commander_path% variable ($env:commander_path in PowerShell syntax).
And the em_test_rar_commander_path is an example of how to utilize that option, if available.

In its turn, the em_test_rar_full_path command is independent of the %commander_path% ($env:commander_path) variable and uses the full path to exe instead.

-c is a "-command" contraction

si is an alias for the Set-Item cmdlet (native PowerShell command):
https://ss64.com/ps/set-item.html
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-item

In the case of the em_test_rar_commander_path command
Set-Item -path env:path -value $env:path';'$env:commander_path\Utils
Set-Item takes %path% (env:path) variable and ads $env:commander_path\Utils (where your rar.exe resides) to its initial value, so we can later use rar.exe without any prefixes.
Note: all the spaces here are within the $env:commander_path variable so we don't care about it at all since PowerShell understands it well.

In the case of the em_test_rar_full_path command
Set-Item -path env:path -value $env:path';'D:\Program' 'Files\Total' 'Commander' '64\Utils
Set-Item takes %path% (env:path) variable and ads D:\Program' 'Files\Total' 'Commander' '64\Utils full path (where your rar.exe resides) to its initial value, so we can later use rar.exe without any prefixes.
Note: to cope with the spaces within the path in that case I enclosed each space within the path into a pair of single quotes like this ' '

Then you just run rar.exe with the commands and switches you wish (here in the examples, rar.exe -? outputs rar.exe help, whereas -? itself can even be omitted here for that purpose).

";pause" in the end allows you to see the results as long as you need it

";" (semicolon) there (before the pause, and at the end of the cmd= line), in the context of PowerShell command-line scripts, is to separate multiple commands on a single line. This allows you to execute several commands sequentially, one after another, within the same line (which fits well into the TotalCommander user-command syntax limitations).

Meanwhile within the $env:path';'D:\Program' 'Files\Total' 'Commander' '64\Utils part ';' (semicolon enclosed within a pair of single quotes) has a different meaning--here it's a delimiter, which separates %path% ($env:path) variable entries regarding its Windows syntax.
https://en.wikipedia.org/wiki/PATH_(variable)#DOS,_OS/2,_and_Windows
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
User avatar
white
Power Member
Power Member
Posts: 5747
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Helping with Alias

Post by *white »

KozakMak wrote: 2024-11-07, 15:40 UTC Trying: *ps "D:\Program Files\Total Commander 64\Utils\Rar.exe" or any path with spaces - get error like "D:\Program"...
What I'm doing wrong?
  1. In Powershell double-quoted strings are expandable strings, so for literal strings you would use single-quoted strings.
  2. You would add the operator & or . at the beginning, otherwise the string is simply displayed.
  3. And when passing commands as arguments to powershell.exe you have to consider parsing of this command line, which strips the double quotes.
So these will work:

Code: Select all

*ps & 'D:\Program Files\Total Commander 64\Utils\Rar.exe'
*ps & '"D:\Program Files\Total Commander 64\Utils\Rar.exe"'
*ps . \"D:\Program Files\Total Commander 64\Utils\Rar.exe\"
Note that the last line contains an expandable string.
User avatar
petermad
Power Member
Power Member
Posts: 16001
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Helping with Alias

Post by *petermad »

Shouldn't it also be:

Code: Select all

param=%A
instead of:

Code: Select all

param=$A
License #524 (1994)
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

petermad wrote: 2024-11-07, 21:50 UTC Shouldn't it also be:

Code: Select all

param=%A
...
That's for sure--all the TotalCommander special parameters such as %WL, %N, %P, %P%N, %S, %P%S, %T, etc., that are intended to be passed from TotalCommander to PowerShell should go in TotalCommander syntax.
The PowerShell-style variables ($etc) become available only after explicitly assigning them as such.
At the same time, the simplest PowerShell assigning, like $variable = 'something', or $variable = $product + $of + $other + $variables within the TotalCommander user-command syntax limitations is broken and unavailable.
A user should use the Set-Variable (alias sv) cmdlet instead like Set-Variable -name variable -value 'something',
or regarding TotalCommander special parameters, a simple example command for testing:

Code: Select all

[em_test_tc_params]
cmd=pwsh -c
param=sv t -value '%T';$t;sv p -value '%P';$p;sv n -value '%N';$n;sv pn -value '%P%N';$pn;sv wl -value '%WL';$wl;pause
https://ss64.com/ps/set-variable.html
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-variable

Where %T,%P,%N,%P%N,%WL are used rather as they are,
while $t,$p,$n,$pn,$wl are ready to further postprocessing in full if needed.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

I made the following script to quickly see how the TotalCommander special parameters are being passed to and seen by PowerShell:
a user-command (usercmd.ini):

Code: Select all

[em_test_tc_params_ps1]
cmd=pwsh -c "%commander_path%\Plugins\app\PowerShell\echoParams.ps1"
param=%WL %N %P %P%N %S %P%S %T %C1 %C2
echoParams.ps1 script:

Code: Select all

'arguments breakdown...'|Write-Host -f Green
''
'raw $args value:'|Write-Host -f Yellow
if ($args){"$args"|Write-Host -f DarkCyan}
else {'... no $args have been passed'|Write-Host -f Red}
''
'process first passed parameter as %WL:'|Write-Host -f Yellow
$param = $args[0]
$param|Write-Host -f DarkCyan
if ($param){
foreach ($line in [System.IO.File]::ReadLines($param)){$line|Write-Host -f Cyan}}
else {'... no $args have been passed'|Write-Host -f Red}
''
'list parameters by individual indexes [0]...[8]:'|Write-Host -f Yellow
$param = $args[0];"[0] %WL  : $param"
$param = $args[1];"[1] %N   : $param"
$param = $args[2];"[2] %P   : $param"
$param = $args[3];"[3] %P%N : $param"
$param = $args[4];"[4] %S   : $param"
$param = $args[5];"[5] %P%S : $param"
$param = $args[6];"[6] %T   : $param"
$param = $args[7];"[7] %C1  : $param"
$param = $args[8];"[8] %C2  : $param"
''
'list all parameters in $args array:'|Write-Host -f Yellow
$total = 0
foreach ($param in $args) {"{0,-8:[0]} : {1}" -f $total,$param;$total++}
''
pause
Where $args is an automatic PowerShell variable that contains an array of values for undeclared parameters that are passed to a function, script, or script block.

https://ss64.com/ps/syntax-args.html
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables#args
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
KozakMak
Senior Member
Senior Member
Posts: 480
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Helping with Alias

Post by *KozakMak »

beb wrote: 2024-11-07, 19:23 UTC Thus, "powershell.exe", or even simply "powershell" would be pretty enough, like this:

Code: Select all

[em_alias_ps_adm]
cmd=powershell -ExecutionPolicy Bypass -NoExit
param=$A

now try with * :)
OS: Win10 | TC: latest x64
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

2KozakMak
It would be great if you could provide an end goal of your command.
It doesn't help much if you post here and there some portions of the info without any details regarding the essence of the issue.
If your goal is just to run the PowerShell window as admin, the solution could be as simple as that:

Code: Select all

[em_test_ps_as_admin]
cmd=powershell -c
param=start-process powershell -verb RunAs
or as a one-liner:

Code: Select all

[em_test_ps_adm]
cmd=powershell -c start powershell -verb RunAs
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
KozakMak
Senior Member
Senior Member
Posts: 480
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Helping with Alias

Post by *KozakMak »

beb wrote: 2024-11-08, 08:21 UTC goal
*ps - run powershell as admin with CURRENT path
*ps .\my script.ps1 arg1 agr2 ... - run powershell script as admin
OS: Win10 | TC: latest x64
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

KozakMak wrote: 2024-11-08, 08:30 UTC .\my script.ps1 arg1 agr2 ... - run powershell script as admin
Ok. Now it's clear. Thank you.
Will see what we can do with that.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

a user-command (usercmd.ini):

Code: Select all

[em_test_ps_adm]
cmd=pwsh -c "c:\script\dummy.ps1"
param='Arg1 Arg2'
a PowerShell script (dummy.ps1):

Code: Select all

# check if the script is elevated
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
# preserve script arguments $args automatic variable as $params string variable for future use
[string]$params = $args
'my script is "{0}"' -f $MyInvocation.MyCommand.Path|Write-Host -f Cyan
'my script arguments are "{0}"' -f $args|Write-Host -f DarkCyan
''
'my script is NOT elevated. Relaunching...'|Write-Host -f Yellow
# relaunch as elevated with the $params as script arguments 
Start-Process pwsh.exe "-File",('"{0}" {1}' -f $MyInvocation.MyCommand.Path,$params) -Verb RunAs
sleep -s 20 # pausing for 20 seconds to see what's on the pre-relaunch screen
exit
}
''
# now running elevated
[string]$params = $args
'now my script IS ELEVATED...'|Write-Host -f Yellow
''
'checking if the script arguments are still there...'|Write-Host -f Yellow
''
'my elevated script is "{0}"' -f $MyInvocation.MyCommand.Path|Write-Host -f Cyan
'my elevated script arguments are "{0}"' -f $params|Write-Host -f Green
''
pause
Notes:
"pwsh" implies I used cross-platform PowerShell (7.4+).
Change it to "powershell" for Windows PowerShell 5.1.

The idea of making a self-elevated script is from here:
https://ss64.com/ps/syntax-elevate.html

To use namely current directory put the user command like this:

Code: Select all

[em_test_ps_adm]
cmd=pwsh -c
param=."\dummy.ps1" 'Arg1 Arg2'
To utilize TotalCommander special parameters (e.g. %P, %WL, etc.) as the script arguments, put it like this:

Code: Select all

[em_test_ps_adm]
cmd=pwsh -c
param=."\dummy.ps1" '%P %WL'
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
KozakMak
Senior Member
Senior Member
Posts: 480
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Helping with Alias

Post by *KozakMak »

2beb
simpler

Code: Select all

[em_alias_ps]
cmd=powershell.exe -ExecutionPolicy Bypass -NoExit
param=-Command "If ('%A') { & .\%A }"

[em_alias_ps_adm]
cmd=*%$SystemX86%\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit
param=-Command "cd '%P'; If ('%A') { & .\%A }"
Last edited by KozakMak on 2024-11-08, 14:26 UTC, edited 1 time in total.
OS: Win10 | TC: latest x64
User avatar
beb
Power Member
Power Member
Posts: 579
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Helping with Alias

Post by *beb »

A slightly updated version of the dummy.ps1 script (an intermediate variable $params no longer used):

Code: Select all

# check if the script is elevated
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
'my script is "{0}"' -f $MyInvocation.MyCommand.Path|Write-Host -f Cyan
'my script arguments are "{0}"' -f $args|Write-Host -f DarkCyan
''
'my script is NOT elevated. Relaunching...'|Write-Host -f Yellow
# relaunch as elevated with the "$args" as script arguments 
Start-Process pwsh.exe "-File",('"{0}" "{1}"' -f $MyInvocation.MyCommand.Path,"$args") -Verb RunAs
sleep -s 20 # pausing for 20 seconds to see what's on the pre-relaunch screen
exit
}
# now running elevated
''
'now my script IS ELEVATED...'|Write-Host -f Yellow
''
'checking if the script arguments are still there...'|Write-Host -f Yellow
''
'my elevated script is "{0}"' -f $MyInvocation.MyCommand.Path|Write-Host -f Cyan
'my elevated script arguments are "{0}"' -f $args|Write-Host -f Green
''
pause
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10/15
Post Reply