In this script, if I use a batch file, it works:
private async void cmdRunBatchFile2_Click(object sender, EventArgs e)
{
cmdRunBatchFile2.Enabled = false;
await Task.Run(() => {
var proc = new Process();
proc.StartInfo.FileName = @"test.bat";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
if (proc.Start())
{
//do something
}
proc.WaitForExit();
});
cmdRunBatchFile2.Enabled = true;
}
However if I change it to test.ps1, it returns this error: System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'test.ps1' with working directory XYZ. The specified executable is not a valid application for this OS platform.'
After reading .Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform", I then blindly try adding
proc.StartInfo.UseShellExecute = true;
This yields another error: System.InvalidOperationException: 'The Process object must have the UseShellExecute property set to false in order to redirect IO streams.'
Do you know why is that?
proc.StartInfo.FileName = @"powershell.exe 'absolute/path/to/test.ps1'";? Weirdly it saysThe system cannot find the file specified.'Do you know why is that?ps.exe. For Windows PowerShell, the single CLI - both for interactive and automated invocations ispowershell.exe(C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe). For PowerShell (Core), the cross-platform, install-on-demand PowerShell edition, the single CLI ispwsh.exe(Windows) /pwsh(Unix-like platforms). While there are standard installation directories if you use the official installers, you're free to install it anywhere, possibly multiple versions side by side.