I have the below code snippet which runs from a C# file in the backend.
using (Process process = new Process())
{
process.StartInfo.FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String cmd = "C:\\Users\\xyz\\AppData\\Local\\Temp\\2\\Demo.ps1";
process.StartInfo.Arguments = cmd;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = true;
StringBuilder error = new StringBuilder();
process.Start();
while (true)
{
bool hasExited = process.HasExited;
String errorStr = null;
while ((errorStr = process.StandardError.ReadLine()) != null)
error.AppendLine(errorStr);
if (hasExited)
break;
Thread.Sleep(100);
}
if (process.ExitCode != 0 || error.Length > 0)
throw new Exception(error.ToString());
}
Demo.ps1
write-host "Working Properly now! "
I want the string to be printed in the same PowerShell window which is open, but somehow it's not showing any output to the opened PowerShell window. If I redirect output to a text file then it is clearly visible.