运行CMD以管理员身份以及命令?命令、管理员、身份、CMD

2023-09-02 21:53:55 作者:日光倾城未必温暖

下面是我的code:

try
{
    ProcessStartInfo procStartInfo = new ProcessStartInfo(
                                            "cmd.exe", 
                                            "/c " + command);
    procStartInfo.UseShellExecute = true;
    procStartInfo.CreateNoWindow = true;
    procStartInfo.Verb = "runas";
    procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command;

    ///command contains the command to be executed in cmd
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

我要保留

procStartInfo.UseShellExecute = true 
procStartInfo.RedirectStandardInput = false;

是否有可能不使用要执行的命令 process.standardinput ? 我尝试执行命令我已经通过了论证,但该命令不执行时。

Is it possible to execute the command without using process.standardinput? I try to execute command I've passed in argument but the command does not executes.

推荐答案

由于@mtijn说你已经得到了很多事情,你以后也覆盖。您还需要确保你逃避的东西正确。

As @mtijn said you've got a lot going on that you're also overriding later. You also need to make sure that you're escaping things correctly.

让我们说你要运行以下命令提升:

Let's say that you want to run the following command elevated:

dir c:

首先,如果你只是跑过此命令的Process.Start()窗口将弹出的打开和关闭的时候了,因为没有什么可以保持窗口打开。它处理命令,然后退出。为了保持窗口打开,我们可以包装在单独的命令窗口中的命令,并使用 / K 开关,以保持它在运行:

First, if you just ran this command through Process.Start() a window would pop open and close right away because there's nothing to keep the window open. It processes the command and exits. To keep the window open we can wrap the command in separate command window and use the /K switch to keep it running:

cmd /K "dir c:"

要运行该命令提升,我们可以使用 runas.exe 就像你只是我们需要逃避的东西多一点。每帮助文档(的runas /?)在命令中的任何报价,我们传递给运行方式需要使用转义一个反斜杠。不幸的是这样做上述命令为我们提供了一个双反斜线是混淆了CMD解析器,这样需要进行转义,太。所以上面的命令,都会变成:

To run that command elevated we can use runas.exe just as you were except that we need to escape things a little more. Per the help docs (runas /?) any quotes in the command that we pass to runas need to be escaped with a backslash. Unfortunately doing that with the above command gives us a double backslash that confused the cmd parser so that needs to be escaped, too. So the above command will end up being:

cmd /K "dir c:\"

最后,使用你提供的,我们可以换一切成一个运行方式命令,然后在另一组引号括起来我们上面的命令语法如下:

Finally, using the syntax that you provided we can wrap everything up into a runas command and enclose our above command in a further set of quotes:

runas /env /user:Administrator "cmd /K "dir c:\""

在命令提示符下运行上面的命令,以确保其按预期工作。

Run the above command from a command prompt to make sure that its working as expected.

鉴于这一切最终code变得更容易组装:

Given all that the final code becomes easier to assemble:

        //Assuming that we want to run the following command:
        //dir c:

        //The command that we want to run
        string subCommand = @"dir";

        //The arguments to the command that we want to run
        string subCommandArgs = @"c:";

        //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
        //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
        string subCommandFinal = @"cmd /K """ + subCommand.Replace(@"", @"\") + " " + subCommandArgs.Replace(@"", @"\") + @"""";

        //Run the runas command directly
        ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
        procStartInfo.UseShellExecute = true;
        procStartInfo.CreateNoWindow = true;

        //Create our arguments
        string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
        procStartInfo.Arguments = finalArgs;

        //command contains the command to be executed in cmd
        using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
        {
            proc.StartInfo = procStartInfo;
            proc.Start();
        }