【问题标题】:Unable to run powershell script in IIS hosted .net web api project无法在 IIS 托管的 .net web api 项目中运行 powershell 脚本
【发布时间】:2020-09-02 03:06:42
【问题描述】:

我正在尝试在.net web api 项目中运行powershell 脚本(脚本用于更新Excel 工作表数据),如下所示,

 var ps1File = @"C:\ActiveLKTel.ps1";
 var startInfo = new ProcessStartInfo()
 {
      FileName = "powershell.exe",
      Arguments = $"-NoProfile -ExecutionPolicy unrestricted \"{ps1File}\"",
      UseShellExecute = false
 };
 Process.Start(startInfo);

当我在 localhost 中运行 .net web api 项目时,这工作正常,但是当我在 IIS 中部署 .net web api 项目时,这不起作用

谁能告诉我如何解决这个问题?

【问题讨论】:

    标签: c# asp.net-mvc powershell iis asp.net-web-api


    【解决方案1】:

    您应该使用-File 参数。

    var ps1File = @"C:\ActiveLKTel.ps1";
    var startInfo = new ProcessStartInfo()
    {
         FileName = "powershell.exe",
         Arguments = $"-NoProfile -ExecutionPolicy unrestricted -File \"{ps1File}\"",
         UseShellExecute = false
    };
    Process.Start(startInfo);
    

    当您在 IIS 中运行脚本时,它不起作用,因为该脚本将在 IIS 应用程序池上的用户上下文中运行。此帐户是否有权:

    • 运行脚本
    • 在脚本中执行操作

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2020-03-09
      • 2019-05-30
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多