简体   繁体   English

从cmd.exe或Windows Run运行命令之间的区别

[英]Difference between running a command from cmd.exe or Windows Run

I'm actually trying to run an application as another user and while it works really nicely in a cmd.exe prompt, it doesn't always work if I go with the Windows Run prompt (it actually depends on what application I'm trying to run). 我实际上是试图以另一个用户身份运行一个应用程序,尽管它在cmd.exe提示符下确实可以很好地工作,但是如果我使用Windows Run提示符,它就不能总是起作用(它实际上取决于我尝试的应用程序跑步)。

For example, this works fine both from cmd.exe or from W-Run prompt (using either Windows XP or Windows 7): 例如,从cmd.exe或W-Run提示符(使用Windows XP或Windows 7)都可以正常工作:

runas /user:ME regedit.exe

While this only works in a cmd.exe prompt (it does ask for my password in both cases but it does nothing after that if launched from W-Run on either WinXP or W7): 虽然这仅在cmd.exe提示符下起作用(在两种情况下均会询问我的密码,但如果从WinXP或W7上的W-Run启动,此操作将不起作用):

runas /user:ME services.msc

It's actually kind of inconsistent, with cmd it always works but with Windows Run, it's really unreliable and random. 它实际上是不一致的,使用cmd可以始终运行,但是使用Windows Run时,它确实不可靠且随机。

Any ideas where there is such a difference? 有什么想法有什么区别吗? To get around the problem, I'm actually using batch files to launch applications as another user and then just type the batch file full path in Windows Run prompt. 为了解决该问题,我实际上是使用批处理文件以另一个用户身份启动应用程序,然后在Windows运行提示下键入批处理文件的完整路径。 It does ensure reliability but I still would like to know if I'm doing something wrong. 它确实确保了可靠性,但是我仍然想知道我做错了什么。

cmd /k "runas /user:ME ""regedit.exe"" && exit"

The "problem" with runas are 符文的“问题”是

  • It needs all the command as only one argument, so if you are running something with arguments you have to enclose all the command in quotes, and if the command includes its own quotes, they need to be escaped. 它只需要所有命令作为一个参数,因此,如果您要运行带有参数的东西,则必须将所有命令括在引号中,并且如果命令包含自己的引号,则必须对其进行转义。

  • It is designed to call .exe files (well, windows valid executable files). 它旨在调用.exe文件(Windows有效可执行文件)。

This two options should handle your program start 这两个选项应该可以处理您的程序启动

runas /user:ME "cmd.exe /c \"start services.msc\""
runas /user:ME "mmc.exe %systemroot%\system32\services.msc"

In the first case, it is using the ability of cmd.exe to find the adecuated executable to run the .msc file. 在第一种情况下,它使用cmd.exe来找到指定的可执行文件以运行.msc文件。 In the second case, it directly calls the adecuated executable to handle the .msc file. 在第二种情况下,它直接调用指定的可执行文件来处理.msc文件。

For your batch files, instead of cmd /k .... & exit , you can directly use cmd /c ... that will close the console when the command finishes. 对于批处理文件,您可以直接使用cmd /c ...代替cmd /k .... & exit ,在命令完成时将其关闭。

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

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