![以不同的用户身份运行以启动 CMD 并运行命令](https://linux22.com/image/1312816/%E4%BB%A5%E4%B8%8D%E5%90%8C%E7%9A%84%E7%94%A8%E6%88%B7%E8%BA%AB%E4%BB%BD%E8%BF%90%E8%A1%8C%E4%BB%A5%E5%90%AF%E5%8A%A8%20CMD%20%E5%B9%B6%E8%BF%90%E8%A1%8C%E5%91%BD%E4%BB%A4.png)
我想通过Windows -> Run
对话框以不同的用户身份运行批处理文件。
我努力了:
runas /profile /user:test CMD "C:\Users\MyName\Desktop\Run.bat"
runas /profile /user:test "CMD "C:\Users\MyName\Desktop\Run.bat""
runas /profile /user:test "CMD \"C:\Users\MyName\Desktop\Run.bat\""
runas /profile /user:test "CMD \"C:\Users\MyName\Desktop\Run.bat"\"
但还是没运气。它打开 CMD 并要求我输入密码。我输入密码,它使用另一个用户名打开另一个 CMD 窗口,但它不会运行蝙蝠。相反,标题栏只是说
管理员:cmd“C:\Users\MyName\Desktop\Run.bat”(以 Home-PC\test 身份运行)
有人可以帮忙吗?
答案1
你缺少一个开关。来自CMD /?
:
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
这些命令应该可以工作。选择适合您需要的那个。
runas /profile /user:test "CMD /C C:\Users\MyName\Desktop\Run.bat"
runas /profile /user:test "CMD /K C:\Users\MyName\Desktop\Run.bat"
如果路径包含空格,请用转义的双引号 ( \"
) 将其括起来:
runas /profile /user:test "CMD /C \"C:\Users\My Name\Desktop\Run.bat\""
runas /profile /user:test "CMD /K \"C:\Users\My Name\Desktop\Run.bat\""