-1

I have a console application and a method that executes a PowerShell script within the console application. So what I'm trying to achieve is running it and then waiting for the result before reading the next method. How would I go about this?

This already outputs the result. I just want to wait for the result to finish rather than jumping to the next method prematurely.

Example/what I'm trying to do

     Process.Start()
        //Wait for result here

The current method is below:

public void ExecutePowershellScript()
{
  var file = @"C:\Path\filename.ps1";
           
            var start = new ProcessStartInfo()
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
                UseShellExecute = false
            };
            Process.Start(start);
}
2
  • Does this answer your question? Output Result from Powershell command to C# variable Commented Aug 5, 2021 at 0:51
  • No. This already outputs the result. I just want to wait for the result to finish rather than jumping to the next method prematurely. I have edited my question. Commented Aug 5, 2021 at 0:54

1 Answer 1

3

First make sure I understand your question. Are you looking for a way to wait until process finishes before continue the program execution ? if yes, there is an easy way:

process.WaitForExit()

Check out documentation here. Execute PowerShell script is actually start a new process. So, to guarantee that the execution has finished is simply waiting for process to exit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.