Running CMD as administrator with argument from C #

I want to run cmd.exe as an administrator with arguments from C # to prevent a UAC popup. This is necessary for use as an unattended installation process. The command I pass is just the path to the installation file (.exe) with /q for silent installation.

When I run this code, a CMD popup appears, but it works as if it is not doing anything.

 public static string ExecuteCommandAsAdmin(string command) { ProcessStartInfo procStartInfo = new ProcessStartInfo() { RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, FileName = "runas.exe", Arguments = "/user:Administrator cmd /K " + command }; using (Process proc = new Process()) { proc.StartInfo = procStartInfo; proc.Start(); string output = proc.StandardOutput.ReadToEnd(); if (string.IsNullOrEmpty(output)) output = proc.StandardError.ReadToEnd(); return output; } } 
+4
source share
5 answers

At least one problem with your team:

 Arguments = "/user:Administrator cmd /K " + command 

Must be:

 Arguments = "/user:Administrator \"cmd /K " + command + "\"" 

In addition, this will not work as a fully automated process, because it will ask for the administrator password, which is unknown in Windows Vista and newer.

+6
source

Those who could not find a solution to their problems found this solution for me: In the solution file, select

Add => New item => Application manifest file

then open it in C #.

enter image description here

In the application manifest file, rename "asInvoker" to "requireAdministrator". At the end, the application manifest file should look like this:

enter image description here

Now create your solution. Then you can open all applications as administrator rights.

+6
source

UAC will appear depending on user preferences in the "User Account Management Settings" section. The program cannot get around this. Only if the user has the Never Notify settings, your program will do what you are trying to do.

+1
source

Two solutions: First, you can use the user's appdata directory. First of all, this saves you from having to use administrator rights. (in a more general approach - think carefully about whether you really need these privileges)

Another solution that we create for a Windows service that will have these privileges. For the first installation of this service, you will need administrator privileges, but after that you can delegate your work to this service.

The second solution is a potential security breach, so you will have to think about what this service will be able to do carefully.

+1
source

I am using this code:

  string[] commands = File.ReadAllLines(commandFile); foreach (string command in commands) { Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); //startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.WorkingDirectory = @"C:\Windows\System32"; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/user:Administrator \"cmd /K " + command + "\""; process.StartInfo = startInfo; process.Start(); } 

As you can see: an attempt to use this code from "Run" in VS will not give an administrator, but if you compile this program and run it from the outside as an administrator, it will. I used this batch file to check the privilege level.

 @echo off goto check_Permissions :check_Permissions echo Administrative permissions required. Detecting permissions... net session >nul 2>&1 if %errorLevel% == 0 ( echo Success: Administrative permissions confirmed. ) else ( echo Failure: Current permissions inadequate. ) pause >nul 
0
source

Source: https://habr.com/ru/post/1480567/


All Articles