I'm traying to execute a Powershell script through C# with administrator privilege.
I've tryied in many ways but with no results.
The best I can do is start a process (System.Diagnostics.Process.Start(p)) with runas Verb. This way, if the user that execute the service is an administrator, I can run my powershell script.
But, what i need is to execute the service with a low privilege user, and, when needed, "login" with the administrator and execute the powershell script.
I've tryied to Impersonate the administrator, but it didn't worked...
The answer Execute PowerShell as an administrator from C# didn't solve the problem, I still can't run my script. The answers are from 2009, i hope someone had solved this kind of issue in the meantime.
// powershell script execution
var processInfo = new System.Diagnostics.ProcessStartInfo
{
Verb = "runas",
LoadUserProfile = true,
FileName = "powershell.exe",
Arguments = scriptPath,
RedirectStandardOutput = true,
UseShellExecute = false
};
var p = System.Diagnostics.Process.Start(processInfo);
runasverb, the second process (likely PowerShell) should trigger a UAC prompt when launched by Windows, which is the standard way to request administrator permissions.