Abid
Abid

Reputation: 690

Calling powershell script from WPF application doesn't execute script

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?

Upvotes: 0

Views: 3165

Answers (1)

Abid
Abid

Reputation: 690

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

Upvotes: 1

Related Questions