How to run multiple lines in cmd as administrator using C#?

738 views Asked by At

Is there a work around or a function that lets me run multiple commands in one elevated command prompt?

I tried setting UseShellExecute=false and use StreamWriter, but I read that if I do that, I can't use an elevated command prompt.

If I set UseShellExecute=true, I can use the elevate cmd but I need to use process.Argument() which only lets me run one command in one cmd process at a time.

I have a loop that runs one command at a time in different cmd process and it works. But it just gets kind of annoying when I have to click 'yes' every time it asks for my permission to run a new line in an elevated cmd. I rather just click 'yes' once and let it run the lines on its own.

Here is a piece of what I got so far:

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
        process.StartInfo = info;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        info.FileName = "cmd.exe";
        info.Verb = "runas";

        int rootDirSize = rootDir.Length;
        for (int i = 0; i < rootDirSize; i++)
        {
            string[] rootFile = Directory.GetFiles(@rootDir[i]);
            int rootFileSize = rootFile.Length;
            for (int j = 0; j < rootFileSize; j++)
            {
                if (rootFile.Contains("/elf") == true)
                {
                    info.Arguments = string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]);
                    //command.WriteLine(string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]));
                }
                else
                {
                    info.Arguments = string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -c {0} {1}", rootFile[j], rootFile[j]);
                    //command.WriteLine(string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -c {0} {1}", rootFile[j], rootFile[j]));
                }


                FileInfo fileSize = new FileInfo(rootFile[j]);
                long bytes = fileSize.Length;
                long mb = bytes / 1024;


                try
                {
                    process.Start();
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    process.WaitForExit();
                    timer.Stop();
                    var time = timer.Elapsed;
                    sw = File.AppendText(logFilePath);
                    {
                        sw.WriteLine(string.Format("{0, -60}{1, -45}{2}", rootFile[j], " Time: " + time, "Size: "+mb+" KB"));
                        sw.Close();
                    }
                }
                catch (Win32Exception ex)
                {
                    process.Close();
                }
            }
        }
2

There are 2 answers

1
magicandre1981 On BEST ANSWER

Run your tool itself as admin, now all opened cmd.exe instances are runing as admin.

To do this, add this entry to your application manifest:

<security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>
</security>

Now you get the UAC dialog only once for your application.

1
laskdjf On

In your arguments specify the user as Adminstrator

info.Arguments = "/user:Administrator \"cmd /K " + string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]) + "\"";