Mehmet
Mehmet

Reputation: 95

Running exe in PowerShell and sending commands line by line

I want to send the arguments I have shown below in order to PowerShell (line by line because they all do separate work and must go in order), but as far as I understand, this method performs the operation in a single line.

How can I solve this situation for send line by line?

ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = @"powershell.exe",             
Arguments = @"@('cd C:\Users\developer\Desktop\Example\', './mc alias set myCloud http://localhost:9000', 'admin', '12345678', './mc admin user add myCloud testwashere 12345678')",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();

Upvotes: 0

Views: 229

Answers (1)

John Nyingi
John Nyingi

Reputation: 1120

You can simply instantiate an Instance of ProcessStartInfo and sequentially trigger commands;

ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = @"powershell.exe",             
Arguments = "",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
};

// then assign arguments as you execute
startInfo.Arguments = @"cd C:\Users\developer\Desktop\Example\";
Process.Start(startInfo);

// keep doing this for all of your arguments
startInfo.Arguments = @"./mc alias set myCloud http://localhost:9000";
Process.Start(startInfo);

// ...

You can add Task.Delay() to space out executions. For more info read this Microsoft Docs

Upvotes: 1

Related Questions