PowerShell的启动工作,等待作业,主线程永远不会从ASP.NET IIS中运行时退出

编程入门 行业动态 更新时间:2024-10-27 14:32:34
本文介绍了PowerShell的启动工作,等待作业,主线程永远不会从ASP.NET IIS中运行时退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前我正在试图建立与PowerShell的螺纹清理脚本,从IIS启动。我做了一个线程由业主杀进程使用PowerShell远程处理,从服务器,我清理脚本的同一列表运行,并且工作没有任何问题。

I am currently trying to build a threaded cleanup script with powershell, initiated from an IIS. I have made a threaded "Kill process by owner" using powershell remoting, running from the same list of servers as my cleanup script, and that works no problem.

$jobs = @() foreach ($comp in $comps) { $jobs += start-job -FilePath ("CleanupJob.ps1") -ArgumentList $comp, $username Write-Host "Started Job on: $comp" } foreach($job in $jobs) { $job | wait-job | out-null receive-job $job.ID } Remove-Job -State Completed

当我运行PowerShell控制台我的剧本,整个事情完全运行,主线程启动28个新的流程,这启动一个远程连接到每台服务器上,并等待所有作业完成。当他们完成后,我得到我的输出和主线程存在。所有按计划进行。

When i run my script from the powershell console, the whole thing runs perfectly, main thread starts 28 new processes, which start a remoting connection to each server, and waits for all the jobs to finish. When they are finished, i get my output and the host thread exists. All running as planned.

并非如此,当我从我的asp应用程序运行它,我得到开始工作在:$补偿,我的每个28服务器,但只有一个结果,从第14名,然后主机线程刚坐在那里。 (直到我用火杀死它,我的输出返回到asp网页)

Not so when I run it from my asp application, i get ""Started Job on: $comp"" for each of my 28 servers, but only a result from the first 14, and then the host thread just sits there. (untill i kill it with fire, and my output is returned to the asp webpage)

我也没办法看到剧本时会发生什么我从asp page.All我可以看到运行它的CPU / RAM的使用下降到相同的水平,当我从PSconsole运行它,但我的主线程永远不会关闭。所以,我相信这个脚本做了假设,但我的网页挂直到主线程关闭(这不可能发生,如前所述)。

I have no way to see what happens in the script when i run it from the asp page.All i can see is cpu/ram usage drop to the same levels as when i run it from the PSconsole, but my main thread never closes. So I do believe the script works as supposed, but my webpage hangs untill the main thread closes(which it never does, as stated).

这是我怎么把我的脚本(而不是pretty .NET 3; PowerShell方法:)

This is how I call my script (not the pretty <3 powershell way:)

public string RunProgramme(string scriptpath, string arguments) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"powershell.exe"; startInfo.Arguments = "& \'"+scriptpath+"\' "+ arguments; //return startInfo.Arguments; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; Process process = new Process(); process.StartInfo = startInfo; process.Start(); string output = process.StandardOutput.ReadToEnd(); string errors = process.StandardError.ReadToEnd(); return output; }

之谜加深,添加此行到我的工作线程

The mystery deepens, added this line to my threaded jobs

Invoke-Command -Session $session -ScriptBlock {param($path) net send mymachine $path} -Args $msg

当我从IIS运行我的剧本,我收到消息,从每台机器。正如我的内存使用情况时,所有的作业运行,但心不是输出正确回来了,我的主人线程只是坐在那里等待... ...

And when i run my script from the IIS, i receive message from each machine. Just as my ram usage shows, all the jobs are run, but the output isnt returned properly, and my host thread just sits there...waiting...

推荐答案

我找到了解决办法。这是造成调用死锁

I found the solution. It's a deadlock caused by calling

string output = process.StandardOutput.ReadToEnd(); string errors = process.StandardError.ReadToEnd();

在对方右。相反,我也跟着msdn.microsoft/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror.aspx#Y95而这样做,而不是:

Right after each other. Instead i followed msdn.microsoft/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror.aspx#Y95 and did this instead:

public string RunProgramme(string scriptpath, string arguments) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"powershell.exe"; startInfo.Arguments = "& \'"+scriptpath+"\' "+ arguments; startInfo.ErrorDialog = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.BeginErrorReadLine(); //Use BeginErrorReadLine on one of the streams to avoid the deadlock //(i didnt need my error stream, but I did need to filter the errors from my output, so i did this) string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(1000 * 60); return output; }

没有比较悬:)

更多推荐

PowerShell的启动工作,等待作业,主线程永远不会从ASP.NET IIS中运行时退出

本文发布于:2023-11-26 22:16:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   主线   工作   PowerShell   IIS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!