0

I try to call a powershell script from within an WPF application (in App.xaml.cs) like this:

private void Application_Startup(object sender, StartupEventArgs e)
{
  var process = new Process();
  process.StartInfo.FileName = "powershell.exe";
  process.StartInfo.Arguments = @"\\WIN-SRV-2019\Betreuung-Release\Install.ps1";

  process.Start();
  process.WaitForExit();
  // ...
}

As a result the powershell window opens up shortly and then closes immediately without executing the script.

When i perform the following command on the command line (CMD):

C:\Users\Entwicklung>powershell \\WIN-SRV-2019\Betreuung-Release\Install.ps1

... everything works fine. The script gets executed as expected.

What am I doing wrong?

4
  • because in your test you are running cmd but in the code you want to run powershell? theres the problem Commented Apr 26, 2019 at 11:40
  • 2
    you can call PowerShell like this, your answer found here: stackoverflow.com/questions/24552404/… Commented Apr 26, 2019 at 11:45
  • Thanks but it should be possible to call powershell using Process, see social.msdn.microsoft.com/Forums/vstudio/de-DE/… Commented Apr 26, 2019 at 11:55
  • It is possible, but it's not the best approach. Commented Apr 26, 2019 at 12:56

1 Answer 1

1

I finally found out what the problem was. I had to add -executionpolicy unrestricted to the arguments:

private void Application_Startup(object sender, StartupEventArgs e)
{
  var process = new Process();
  process.StartInfo.FileName = "powershell.exe";
  process.StartInfo.Arguments = @"-executionpolicy unrestricted \\WIN-SRV-2019\Betreuung-Release\Install.ps1";

  process.Start();
  process.WaitForExit();
  // ...
}
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.