Verb = "runas" not run as elevated - c#

As discussed in other post, I came to know that Verb = "runas" works as elevated.
I need to run "logman.exe" arguments with Elevated privileged. With below code, I am not getting any output,
try
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "logman.exe",
Arguments = "PerfCounterCustom",
Verb = "runas",
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
process.Start();
string lineData;
while ((lineData = process.StandardOutput.ReadLine()) != null)
{
if (lineData.Contains("Root Path:"))
{
Console.WriteLine(lineData.Trim());
}
}
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Note - When I am running above EXE, right click as Admin, I m getting the output.
What changes required so that I can make Elevated through code in C# and output?

Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation.
Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working
UseShellExecute=false will allow you to capture Standard Output and Standard Error messages.

Related

How can I interact with shell responses with .NET Core console app?

I am writing a CLI app to hide some of the details of running commands in the shell/terminal.
With the code below I can execute a single command ssh, for example:
var result = RunCommand("ssh", "100.100.109.86");
static string RunCommand(string command, string args)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
return string.IsNullOrEmpty(error) ? output : error;
}
My question is how can implement a loop so that I can output the result of the command to the user and enable them to execute another command (or more)?
For example, the response to the above ssh command might as the to ask them if they are sure they want to connect inviting them to respond with (yes/no/[fingerprint])

C#: Process.Start - The requested operation requires elevation

I am trying to run an exe file from my own machine:
string versionInFolder = #"c:\test.exe";
public void Install(string versionInFolder)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
FileName = versionInFolder,
WindowStyle = ProcessWindowStyle.Hidden,
};
using (Process process = Process.Start(processStartInfo))
{
process.WaitForExit();
}
}
This file exist and can run manually but i got this error:
System.ComponentModel.Win32Exception: 'The requested operation
requires elevation'
I found this post but did not understand the reason for this error and how to solve it.
You need to run your programm as admininistrator.
Check that first.
And if that doesn't work, or if you're the administrator, try to move your file in another place.

Getting execption whenever it runs from process.start

so im having this weird issue with a win 7 pc. I have this application that runs this powershell script from a .bat file.
This is the code:
public void GetLastestEarthBackground()
{
var directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
var vbsFile = Path.Combine(directoryPath, Settings.VBSFileName);
if (File.Exists(vbsFile))
{
var process = new Process
{
StartInfo =
{
WorkingDirectory = directoryPath,
Verb = "runas",
UseShellExecute = true,
FileName = "run.bat", //Settings.VBSFileName,
Arguments = "//B //Nologo"
}
};
process.Start();
process.WaitForExit();
}
}
When i run it from my application i see this in the command line:
But if i double click on the batch file i get this resut:
Any ideas?
This seems like a issue with user rights, the program running the script probably does not have the same rights as your user.

Using Process.Start() with IE and waiting for web response

Is there a way to capture a response from the Process class using it to launch Internet Explorer.
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Program Files\Internet Explorer\iexplore.exe",
Arguments = "http://www.google.com",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
System.Threading.Thread.Sleep(3000);
proc.Kill();
I tried :
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// do something with line
}
but IE sends back an immediate response of hey, i opened and called the URL. Anyone have any ideas?
There is no way of getting information from IE when it is running in a separate process.
However, you can run it inside of a Web Browser control in your own Form. IE then exposes a number of events that you can listen to.

How to launch an external program (process) as a different user from .net

In my .net web application, I need to launch another program as a new process - under a different user account (one with higher priviledges). My code runs without error, but I never get any output (I've tried Redirecting the output), and the program doesn't seem to execute at all. Its not an error with the User auth details, I've tried a bogus username and sure enough it throws a valid exception in that case. Nothing gets added to the event log.
SecureString passwordString = new SecureString();
foreach (char c in "MyPassword")
{
passwordString.AppendChar(c);
}
var process = new System.Diagnostics.Process
{
StartInfo =
{
UserName = "myuser", Password = passwordString, Domain = "MyDomain",
WorkingDirectory = HttpContext.Request.MapPath("~/bin"),
UseShellExecute = false,
FileName = HttpContext.Request.MapPath("~/bin") + "\\ServerCertificateImporter.exe",
Arguments = instanceLocationId.ToString(),
CreateNoWindow = true,
RedirectStandardInput = false,
RedirectStandardOutput = false,
RedirectStandardError = false
}
};
process.Start();
process.WaitForExit();
process.Close();
As Yahia pointed out, this was caused by the fact that I was trying to call a program with a different user account - and windows didn't want me to do that.

Categories

Resources