从WPF应用程序调用Powershell脚本不执行脚本

时间:2019-04-26 11:38:11

标签: c# wpf powershell

我尝试从WPF应用程序(在 App.xaml.cs 中)调用Powershell脚本,如下所示:

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

结果是Powershell窗口很快打开,然后立即关闭而不执行脚本。

当我在命令行(CMD)上执行以下命令时:

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

...一切正常。该脚本将按预期执行。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

我终于发现了问题所在。我必须在参数中添加-executionpolicy unrestricted

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();
  // ...
}
相关问题