简体   繁体   中英

Can't run batch file from runas command

I'm trying to open a file using runas command from cmd.

I succeed when i run it like this:

file.bat

or like this:

start file.bat

but when i run:

runas /user:username file.bat

the file opens but closes immediately (I'm entering the right password). I know that for sure that I'm entering the right password and that the batch file opens and closes without executing its content.

Any help please?

I don't know how to get the runas command to run in the same window. But a trick is to spawn the cmd window yourself with the /k switch to prevent it from closing when done:

runas /user:username "cmd /k {fullpath}\\file.bat"

Note that you need the full path to the file because the new window opens to your user directory.

Or you just put pause at the end of your .bat file instead of doing the above.

Use PsExec instead (MS SysInternals suite). Much better and secure (password used).

psexec -user Administrator -p Passwd "xcopy file.bat {fullpath}\\file.bat"

Use PsExec instead (MS SysInternals suite). Much better and secure (password >used).

psexec -user Administrator -p Passwd "xcopy file.bat {fullpath}\\file.bat"

Thanks! but I'm looking for a solution that will run portably, without installation...

I don't know how to get the runas command to run in the same window. But a trick is to spawn the cmd window yourself with the /k switch to prevent it from closing when done:

runas /user:username "cmd /k {fullpath}\\file.bat"

Note that you need the full path to the file because the new window opens to your user directory.

Or you just put pause at the end of your .bat file instead of doing the above.

I did put the pause command at my batch file, but an error occurred before it so the file terminated. Thanks a lot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM