从ASP.NET(C#)调用Powershell脚本,但不等待结果
本文关键字:脚本 结果 等待 Powershell 调用 NET ASP | 更新日期: 2023-09-27 18:28:07
我有一个powershell脚本,我想从网页调用它。我有c#代码,它可以工作,但脚本最终会执行一些sql任务,而这些任务的回报并不快。它没有任何我想显示在网页上的结果,所以我似乎无法让网页启动脚本,然后继续前进。它一直在我的浏览器底部显示"正在等待服务器…",即使没有任何可返回的内容。最终页面超时。
我尝试过使用InvokeAsync()选项,但它仍然给我相同的结果。使用Process.Start()调用Powershell脚本然后继续操作会有更好的运气吗?谢谢Greg
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
string cmdArg = scriptfile + " " + TextBox4.Text + " " + RadioButtonList1.SelectedValue;
//Collection<PSObject> psresults;
using (Pipeline pipeline = runspace.CreatePipeline())
{
pipeline.Commands.AddScript(cmdArg);
//psresults = pipeline.Invoke();
pipeline.InvokeAsync(); // InvokeAsync calls it as a background process so it won't make the web page timeout.
}
runspace.Close();
您异步使用运行空间管道,但您同步打开了运行空间
尝试使用:
runspace.OpenAsync();
希望有所帮助:)
您不应该将运行空间放在using子句中——它会在invokeasync调用后立即处理它,并阻止它。相反,您应该在回调中处理运行空间。