Home > Enterprise >  I made my own sudo command in windows, but if i run a program via file-path then it doesnt count the
I made my own sudo command in windows, but if i run a program via file-path then it doesnt count the

Time:04-13

if i type "sudo "C:\Whatever with space\file.txt"" then the output is "The command "C:\Whatever" is either misspelled or could not be found."

set "sz=%2 %3 %4 %5 %6"
if /I "%c%" EQU "" goto cmd
powershell.exe -Command "Start-Process cmd \"/k %sz%\" -Verb RunAs"
goto exit
:cmd
powershell.exe -Command "Start-Process cmd -Verb RunAs"
goto exit

CodePudding user response:

PowerShell uses the backtick character instead of the backslash to escape special characters. Try replacing the backslashes used within the command string sent to PowerShell.

powershell.exe -Command "Start-Process cmd `"/k %sz%`" -Verb RunAs"

CodePudding user response:

The "quirks" of cmd.exe's parameter parsing make a robust solution really difficult; here's a solution that:

  • Starts the elevated (run-as-admin) cmd.exe session in the same working directory as the caller.

  • Supports passing commands with the following limitations:

    • Passing a single command and its argument should work as-is, even with quoted command names / arguments; e.g.:

      :: Also works if the arguments contain spaces.
      sudo "notepad.exe" "c:\windows\win.ini"
      
    • Multiple commands, incorporating operators such as &, | and >, require escaping the operators with ^^^ (sic).

      sudo echo "hi there" ^^^| findstr "there"
      
@echo off & setlocal

set args=%*
set "wd=           
  • Related