Powershell 脚本已执行,但从 IIS ASP.NET 站点调用时挂起
这是我的代码:
在 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
我尝试过:
- 当 asp/ps1 发布到 IIS 并从浏览器触发时,
- ExportTestResult.ps1 已使用新进程 ID 启动;
- 文件(参考ExportTestResult.ps1中的$outfile)已创建;
- powershell进程保持运行而不退出。
- 当我在Windows ProcessManager中杀死powershell进程时,浏览器立即显示下载进度
- 当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:
- When the asp/ps1 published to IIS and trigger from browser,
- ExportTestResult.ps1 has been started with a new process id ;
- The file (refer to $outfile in ExportTestResult.ps1) has been created;
- powershell process keep running without exit.
- When I killed the powershell process in windows ProcessManager, the browser show the downloading progress immediately
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论