Powershell 脚本已执行,但从 IIS ASP.NET 站点调用时挂起

发布于 2025-01-10 21:19:04 字数 2229 浏览 0 评论 0原文

这是我的代码:

在 Default.aspx.cs

//Default.aspx.cs is used to create a docx file on server 
//and push to the browser for downloading  
protected void btnSubmit_Click(object sender, EventArgs e)
{
  try
  {
    string strOutFile = string.Empty;
    switch (ExportType.SelectedItem.Value)
    {
      case "TestReport":
        //_strBaseDirectory was defined in web.config and refer to the site wwwroot folder
        strOutFile = _strBaseDirectory + @"Download\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".docx";
        RunPowershell(
          _strBaseDirectory + @"Powershell\ExportTestResult.ps1 " + "' -Outfile " + strOutFile);
        break;
      default:
        break;
    }
    Download("~/DownLoad/" + new FileInfo(strOutFile).Name);
  }
  catch (Exception ex)
  {
    //log   
  }  
}

private void Download(string strFilePath)
{
  if (new FileInfo(Server.MapPath(strFilePath)).Exists)
  {
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + new FileInfo(strFilePath).Name);
    Response.TransmitFile(Server.MapPath(strFilePath));
    Response.End();
  }
} 

private int RunPowershell(string strArguments)
{
  try
  {
    var p = Process.Start(
      "powershell.exe",
      @strArguments
    );
    p.WaitForExit();
    return p.ExitCode;
  }
  catch (Exception ex)
  {
    throw (ex);
  }
}

Powershell\ExportTestResult.ps1 中:

#For debugging, all the suspected cmdlets and COM reference operations have been removed one-by-one. 
#The latest and entire content(identical to my local one), I copy here

param ($Outfile)
cls

cp "d:\wwwroot\site\download\template.docx" $outfile
exit

我尝试过:

  1. 当 asp/ps1 发布到 IIS 并从浏览器触发时,
    1. ExportTestResult.ps1 已使用新进程 ID 启动;
    2. 文件(参考ExportTestResult.ps1中的$outfile)已创建;
    3. powershell进程保持运行而不退出。
    4. 当我在Windows ProcessManager中杀死powershell进程时,浏览器立即显示下载进度
  2. 当asp project/ps1直接在VS中运行时,ExportTestResult.ps1和p.WaitForExit()直接退出。

环境:

  • IIS: 10
  • PowerShell: 5.1

那么,如何让powershell进程在IIS中像在VS中调试一样正确退出呢?我的 IIS 配置有问题吗?

如果我错过了什么,请告诉我。

This is my code:

In Default.aspx.cs

//Default.aspx.cs is used to create a docx file on server 
//and push to the browser for downloading  
protected void btnSubmit_Click(object sender, EventArgs e)
{
  try
  {
    string strOutFile = string.Empty;
    switch (ExportType.SelectedItem.Value)
    {
      case "TestReport":
        //_strBaseDirectory was defined in web.config and refer to the site wwwroot folder
        strOutFile = _strBaseDirectory + @"Download\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".docx";
        RunPowershell(
          _strBaseDirectory + @"Powershell\ExportTestResult.ps1 " + "' -Outfile " + strOutFile);
        break;
      default:
        break;
    }
    Download("~/DownLoad/" + new FileInfo(strOutFile).Name);
  }
  catch (Exception ex)
  {
    //log   
  }  
}

private void Download(string strFilePath)
{
  if (new FileInfo(Server.MapPath(strFilePath)).Exists)
  {
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + new FileInfo(strFilePath).Name);
    Response.TransmitFile(Server.MapPath(strFilePath));
    Response.End();
  }
} 

private int RunPowershell(string strArguments)
{
  try
  {
    var p = Process.Start(
      "powershell.exe",
      @strArguments
    );
    p.WaitForExit();
    return p.ExitCode;
  }
  catch (Exception ex)
  {
    throw (ex);
  }
}

Powershell\ExportTestResult.ps1 :

#For debugging, all the suspected cmdlets and COM reference operations have been removed one-by-one. 
#The latest and entire content(identical to my local one), I copy here

param ($Outfile)
cls

cp "d:\wwwroot\site\download\template.docx" $outfile
exit

What I have tried:

  1. When the asp/ps1 published to IIS and trigger from browser,
    1. ExportTestResult.ps1 has been started with a new process id ;
    2. The file (refer to $outfile in ExportTestResult.ps1) has been created;
    3. powershell process keep running without exit.
    4. When I killed the powershell process in windows ProcessManager, the browser show the downloading progress immediately
  2. When the asp project/ps1 ran in VS directly, ExportTestResult.ps1 and p.WaitForExit() exited directly.

Environment:

  • IIS: 10
  • PowerShell: 5.1

So, how to let the powershell process exit correctly in IIS just like debugging in VS? Is there something wrong with my IIS config?

If I missed something, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文