I know i am waaaay (7 years) late but this is what i found works. This peace of code basically runs the program specified as system user.
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = $"/Create /RU \"NT AUTHORITY\\SYSTEM\" /RL HIGHEST /SC ONCE /TN MyTask /TR \"{program} {programArgs}\" /ST {DateTime.Now.AddSeconds(5):HH:mm:ss} /F",
Verb = "runas", // Run the process as an administrator
CreateNoWindow = false,
UseShellExecute = false
};
Process process = Process.Start(startInfo);
process.WaitForExit();
Console.WriteLine(process.ExitCode);
startInfo = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = $"/Run /TN MyTask",
Verb = "runas", // Run the process as an administrator
CreateNoWindow = true,
UseShellExecute = false
};
process = Process.Start(startInfo);
process.WaitForExit();
Console.WriteLine(process.ExitCode);
startInfo = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = $"/Delete /TN MyTask /F",
Verb = "runas", // Run the process as an administrator
CreateNoWindow = true,
UseShellExecute = false
};
process = Process.Start(startInfo);
process.WaitForExit();
Console.WriteLine(process.ExitCode);
Console.ReadLine();
This code will execute any program with any args as nt authority\system. Keep in mind it is incompatible with window display (will allways run in the background). We can verify if this works by defining a cmd to run, that creates a file with the output of "whoami":
string program = Environment.GetEnvironmentVariable("ComSpec"); // Path to cmd.exe
string programArgs = $"/C whoami > \"C:\\Users\\{Enviroment.UserName}\\Desktop\\text.txt\"";
You can now find your text.txt file on your desktop with the text nt authority\system
in it