The following code executes the same code via both processes:
internal class Program
{
private static void Main()
{
const string COMMAND = @"SCHTASKS /QUERY /XML ONE";
Collection<PSObject> psObjects;
using (var ps = PowerShell.Create())
{
ps.AddScript(COMMAND);
psObjects = ps.Invoke();
}
var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = "cmd.exe",
Arguments = "/C " + COMMAND
}
};
process.Start();
string cmdTasks;
using (var reader = process.StandardOutput)
{
cmdTasks = reader.ReadToEnd();
}
}
}
The response object from the two approaches differs. The Process.Start()
approach returns a string, which I would have to parse whereas invoking PowerShell gives me a collection of PSObject
objects.
solved Batch file v PowerShell script