简体   繁体   English

无法通过runas命令运行批处理文件

[英]Can't run batch file from runas command

I'm trying to open a file using runas command from cmd. 我正在尝试使用cmd中的runas命令打开文件。

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. 我不知道如何让runas命令在同一窗口中运行。 But a trick is to spawn the cmd window yourself with the /k switch to prevent it from closing when done: 但是,有一个技巧是使用/ k开关自己生成cmd窗口,以防止它在完成后关闭:

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. 或者,您只需在.bat文件的末尾放置pause ,而不执行上述操作。

Use PsExec instead (MS SysInternals suite). 请改用PsExec(MS SysInternals套件)。 Much better and secure (password used). 更好,更安全(使用了密码)。

psexec -user Administrator -p Passwd "xcopy file.bat {fullpath}\\file.bat" psexec-用户管理员-p Passwd“ xcopy file.bat {fullpath} \\ file.bat”

Use PsExec instead (MS SysInternals suite). 请改用PsExec(MS SysInternals套件)。 Much better and secure (password >used). 更好,更安全(使用密码>)。

psexec -user Administrator -p Passwd "xcopy file.bat {fullpath}\\file.bat" psexec-用户管理员-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. 我不知道如何让runas命令在同一窗口中运行。 But a trick is to spawn the cmd window yourself with the /k switch to prevent it from closing when done: 但是,有一个技巧是使用/ k开关自己生成cmd窗口,以防止它在完成后关闭:

runas /user:username "cmd /k {fullpath}\\file.bat" runas / user:用户名“ 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. 或者,您只需在.bat文件的末尾放置暂停,而不执行上述操作。

I did put the pause command at my batch file, but an error occurred before it so the file terminated. 我确实在我的批处理文件中添加了pause命令,但是在此之前发生了错误,因此文件终止了。 Thanks a lot! 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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