0

If I run this command, after entering my password, all I see is a shortly blinking new cmd window, then nothing.

runas /user:<username> batch_file.bat

My batch file contains a pause command which should therefore require me to press Enter, and so I would at least expect the cmd window to persist until that point in my batch file.

Am I doing anything wrong? How can this be fixed?

2
  • The runas is supposed to ask for the password before running the batch file. Does this username have a password? Does it work from an elevated CMD?
    – harrymc
    Commented Jul 29, 2022 at 10:10
  • It asks for a password, that seems fine.
    – Charles
    Commented Jul 29, 2022 at 13:33

3 Answers 3

2

Run runas /user:<user> "cmd /k batch_file.bat" and the error should become obvious:
batch_file.bat isn't even found, because the new process is run in the folder C:\WINDOWS\system32, but your script surely is stored somewhere else. Give it an absolute path:

runas /user:<user> "cmd /k "F:\ull path\to\batch_file.bat""

or in your short version:

runas /user:<user> "F:\ull path\to\batch_file.bat"
3
  • I actually came up with something similar on my own : runas /user:<user> ".\batch_file.bat & pause" but to the same effect of leaving the window open after runnign and indeed saw that runas runs out of C:\WINDOWS\system32 as you said. Thanks though !
    – Charles
    Commented Aug 1, 2022 at 8:06
  • ".\batch_file.bat" is still a relative path (relative to the current working folder), which in this case results in an absolute path of C:\Windows\system32\batch_file.bat
    – Stephan
    Commented Aug 1, 2022 at 8:48
  • Yep that was the issue, which I discovered with the method mentionned in my other comment.
    – Charles
    Commented Aug 5, 2022 at 12:59
0

If a batch script contains the pause command, but executing the script flashes but then not pauses is caused if there is a syntax error in the script.

For example, your script uses if exists filename echo test,
but in reality it is if exist filename echo test the script will give a syntax error and not execute properly.

To troubleshoot your script, open a command prompt, navigate to the script, and type in the command you want to test, for example: mybatch.cmd.

This will display the error in the script and return to the existing command prompt, keeping the error on screen.

The Runas will always launch it in a new window but as long as there's no syntax error in the script, which was tested in the previous step, the pause will work there. In that case you can now try and use: runas /user:myuser mybatch.cmd.

2
  • Hm, I mean the script in the batch file works when running from a command line - what I mean is, it achieves its functional purpose. That some of the commands along the script return error, is totally possible - but also not at all a functional problem. The script just tries a few different things in a sequence, some of which will work, which is good enough for me.
    – Charles
    Commented Jul 29, 2022 at 12:00
  • I removed what I thought might be the error-returning commands. No change.
    – Charles
    Commented Jul 29, 2022 at 12:05
0

Seems like runas is returning some error, which you don't see, instead of running the .bat file.

You can at least see the error if you issue the command from a cmd window. Then you can react to the provided error.

To see the error you need to issue the runas ... command from a cmd window - to know what is the error, fix it and later you will be able to run the batch file correctly. You should receive pause prompt for a keystroke.

Surely if you don't get pause message and computer does not wait for keystroke - that means it does not run the batch file.

5
  • Issue WHICH command from a cmd window ?
    – Charles
    Commented Jul 29, 2022 at 9:46
  • The command in the batch file. Or, you could add a pause at the end of the batch file to see if it's getting called at all. Commented Jul 29, 2022 at 10:07
  • I don't understand. There isn't a single command in my batch file, to be sure. And, if the one pause in the middle doesn't get executed (or not the way I think it should), why would one at the end ?
    – Charles
    Commented Jul 29, 2022 at 11:58
  • No, you need to issue runas command with all options in cmd window to see what error appears. If pause in batch file is not working - it is telling that the batch file is not started.
    – pbies
    Commented Jul 29, 2022 at 13:01
  • No but to be clear : issuing the runas command with all my options from the cmd is already what I'm doing, with the symptoms described. However you were right that there was an error involved.
    – Charles
    Commented Jul 29, 2022 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .