Run Command as another user C# - c#

I have an app running as Network Service (I can't change this) and need to run a command (execute as bat script) as a known local user. I seem to get no response and the script doesn't execute. The odd thing is that if i run the code as the local user its works without an issue.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\Windows\System32\cmd.exe";
startInfo.Arguments = "/C " + #"c:\example\script.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
var sspw = new SecureString();
foreach (var c in "MyPassword")
{
sspw.AppendChar(c);
}
startInfo.Domain = Environment.MachineName;
startInfo.UserName = "MyUser";
startInfo.Password = sspw;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

I believe you need to set the working directory when using a Username/Password
according to MSDN...
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.

Not sure whether this solution will help your need but if set the WorkingDirectory to current directory (".") then it should work. Basically the user who is running the process should have permission on the folder that you mention.

Related

CMD Command doesn't execute [duplicate]

This question already has an answer here:
Run process as administrator from a non-admin application
(1 answer)
Closed 1 year ago.
I'm trying to run this command in cmd from c# but for some reason it doesn't work. But when I change my Argument to "/C ipconfig" it work perfectly. I don't understand why. What am I doing wrong?
Here is my code:
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/C wbadmin start backup - backupTarget:{TargetBackupDrive}: -include:{LocationOs}: -allcritical - quiet";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Verb = "runas";
process.Start();
CmdOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
The verb "runas" does not work when UseShellExecute is set to false as it is pointed out here: Set ProcessStartInfo.EnvironmentVariables when Verb="runas"
You could either provide admin credentials with the UserName and Password properties our set UseShellExecute to true and execute your command in the shell.

How to start a c# process as another user?

I want to start a c# process as another user.
I know the verb "runas" but that only accepts admin user creds.
//This code actually meant to restart the appplication with specific args
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = args;
startInfo.Verb = "runas"; //this line is the question
Process p = Process.Start(startInfo);
I want to achieve the same thing but also with non-admin users.

How do I correctly start a process with administrator rights?

So I've looked at alot of different SO posts, I've been on codeproject and dreamincode aswell but I cant for the life of me find out how to CORRECTLY start a process on Windows 8.1 with admin rights.
This is my code.
I'm trying to create a silent install of FireFox but everytime I give it the filepath it still promts me with the UAC, and I thought that running as admin would work. What am I doing wrong here?
Console.WriteLine("Please enter the path to the application: ");
string path = Console.ReadLine();
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.Verb = "runas";
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = path;
Process.Start(psi);

Why is dumping MySQL database programmatically different from dumping via command line?

To dump database from command line, all I need to do is:
mysqldump -uroot --password= myDb --routines> "C:\s.sql"
So all I would try programmatically is this, which is the direct interpretation of it I suppose:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = "-uroot --password= myDb --routines> \"C:\\s.sql\"";
Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
Which doesn't work at all. Instead I have to go for this which one can find all over the net, which works too.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format("-R -u{0} --password={1} -h{2} {3} --routines", "root", "", "localhost", "myDb");
Process process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
using (StreamWriter writer = new StreamWriter("C:\\s.sql"))
{
writer.WriteLine(output);
writer.Close();
}
Why is that I need to use stream writer to get the database in an sql file which I can do otherwise directly from commands in command prompt?
What is the role of -R in the second block?
You can not redirect stdout using ">" in arguments because that is a feature of the command prompt.
-R includes the stored procedures and functions in the dump. See http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines for more information.
What you are doing in the command line version is using the shell to pipe the standard output to a file (the > command, followed by a file name, is a shorthand way of saying "take all of the standard output of this program and write it to this file"). To do the same thing from C#, you need to hand the standard output yourself and write it to a file.
The -R in the second example seems duplicative. According to this page, it is the same as --routines. Have you tried it without?
I thought I would include what the Arguments could look like programmatically, in our case we also wanted to dump the events of the DB to a file.
psi.Arguments = string.Format(#"-u{0} -p{1} -h{2} {3}", "someUser", "xxxxxx", "localhost", dbName, "--routines","--events");

How to pass command line argument in .net windows application

i want to pass a argument in c#.net to a console application i tried ProcessStartInfo
but that can be used for immediate run of an application ... but i want to set the arguments for the application which will run at scheduled time
Use the arguments propery to pass command line arguments
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx
Example:
var info = new System.Diagnostics.ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = "/C";
info.UseShellExecute = true;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();

Categories