0
试图异步调用ASP.NET IHttHandler
中的powershell脚本,我偶然发现了http://msdn.microsoft.com/en-us/library/ee706580(v=vs.85).aspx上的示例。我自己的代码如下所示:异步调用ASP.NET中的powershell脚本
using (PowerShell powershell = PowerShell.Create()) {
powershell.AddScript("1..10 | foreach {$_ ; start-sleep -milli 500}");
PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
output.DataAdded += delegate(object sender, DataAddedEventArgs e) {
PSDataCollection<PSObject> myp = (PSDataCollection<PSObject>)sender;
Collection<PSObject> results = myp.ReadAll();
};
powershell.InvocationStateChanged += delegate(object sender, PSInvocationStateChangedEventArgs e) {
if (e.InvocationStateInfo.State == PSInvocationState.Completed) {
// Clean up
}
};
IAsyncResult asyncResult = powershell.BeginInvoke<PSObject, PSObject>(null, output);
}
不幸的是,powershell脚本显然没有被执行。任务管理器中没有显示PowerShell进程。
我用DataAdded
和IncovationStateChanged
处理程序设置了断点。第一个从来没有被调用,第二个显示,e.InvocationStateInfo.State
永远不会变成Completed
,但以Stopped
结束。
我在做什么错?
此方法不会启动PowerShell进程。您有PowerShell引擎正在运行。通过使用BeginInvoke,相同的进程内引擎在另一个线程上执行异步请求,而不在另一个进程中执行请求。在某些时候,你应该调用EndInvoke()。 –
我想你是指我的评论,没有PowerShell过程显示?但是,脚本仍未执行。 “InvocationStateInfo.State”立即从“running”变为“Stopping”,但该脚本预计将运行2:30分钟。 – sk904861