以管理员身份运行cmd和命令

本文关键字:命令 cmd 运行 管理员 身份 | 更新日期: 2023-09-27 18:10:11

下面是我的代码:

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是否可以执行该命令?我试图执行我传入参数的命令,但命令没有执行。

以管理员身份运行cmd和命令

正如@mtijn所说,您有很多事情要做,稍后还要重写。您还需要确保转义正确。

假设您想要运行以下命令:

dir c:'

首先,如果您只是通过Process.Start()运行这个命令,一个窗口将立即弹出并关闭,因为没有什么可以保持窗口打开。它处理命令并退出。为了保持窗口打开,我们可以将命令包装在单独的命令窗口中,并使用/K开关保持其运行:

cmd /K "dir c:'"

要运行该命令,我们可以像您一样使用runas.exe,只是我们需要更多地逃避一些事情。根据帮助文档(runas /?),我们传递给runas的命令中的任何引号都需要用反斜杠转义。不幸的是,用上面的命令这样做会给我们一个双反斜杠,这会混淆cmd解析器,所以也需要转义。所以上面的命令最终会是:

cmd /K '"dir c:'''"

最后,使用您提供的语法,我们可以将所有内容包装成runas命令,并将上述命令括在一组进一步的引号中:

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

在命令提示符下运行上述命令以确保其正常工作。

考虑到所有这些,最终代码变得更容易组装:

        //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();
        }

为什么要用参数初始化进程对象,然后再重写这些参数?顺便说一句:你设置参数的最后一点,你将'command'连接到'cmd',这没有多大意义,可能是它失败的地方(看起来你错过了一个空格)。

另外,如果您当前使用的是标准命令行,那么您可能需要考虑使用runas工具。您也可以从命令行调用runas。

还有,为什么你要从命令行运行'command' ?为什么不直接从进程中启动它呢?从提供的管理权限开始?下面是一段伪代码:

Process p = Process.Start(new ProcessStartInfo()
{
    FileName = <your executable>,
    Arguments = <any arguments>,
    UserName = "Administrator",
    Password = <password>,
    UseShellExecute = false,
    WorkingDirectory = <directory of your executable>
});