无法 运行 来自 运行 作为命令的批处理文件

Can't run batch file from runas command

我正在尝试使用 运行as 命令从 cmd 打开一个文件。

当我 运行 这样我就成功了:

file.bat

或者像这样:

start file.bat

但是当我 运行:

runas /user:username file.bat

文件打开但立即关闭(我正在输入正确的密码)。 我知道我肯定输入了正确的密码,并且批处理文件在不执行其内容的情况下打开和关闭。

有什么帮助吗?

我不知道如何在同一个 window 中将 runas 命令发送到 运行。但是一个技巧是使用 /k 开关自己生成 cmd window 以防止它在完成后关闭:

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

请注意,您需要文件的完整路径,因为新 window 打开您的用户目录。

或者您只是将 pause 放在 .bat 文件的末尾,而不是执行上述操作。

改用 PsExec(MS SysInternals 套件)。更好更安全(使用密码)。

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"

谢谢!但我正在寻找一种无需安装即可 运行 便携的解决方案...

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.

我确实在我的批处理文件中放置了暂停命令,但在它之前发生了一个错误,因此文件终止了。非常感谢!