I am attempting to run a PS script from C# with Powershell as a Process, like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = configFile.AppSettings.Settings["PSDir"].Value.ToString() + @"\powershell.exe";
startInfo.Arguments = @"& '" + scriptPath + "'\"";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
return process;
I call that method like this:
Process proc = RunPSScript(startupPath + "\\stats.ps1");
proc.WaitForExit();
The program gets stuck at:
proc.WaitForExit();
I can tell the initial part of the script completes but nothing beyond that. It is not the script that is bad because I have run it outside of the code and it runs perfectly fine.
When I remove the WaitForExit(), the process call exists right away and the program keeps going, but that's no good because I need to have the script generate data before the rest of the program can keep going.
Curiously, if I end the debug session after having started the PS call, the script keeps running and whatever was locking up its completion is now gone and it continues running to completion.
Any ideas appreciated.
Thanks!