目前我正在试图建立与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中运行时退出
发布评论