Can't run batch file from runas command

喜你入骨 提交于 2019-12-10 11:46:27

问题


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?


回答1:


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.




回答2:


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

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




回答3:


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!



来源:https://stackoverflow.com/questions/35733752/cant-run-batch-file-from-runas-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!