How do I debug a PowerShell background job?

吃可爱长大的小学妹 提交于 2019-12-24 10:16:22

问题


Jason Archer helped me solve my last issue, but in testing his solution I stumbled across the real problem. My server won't run chained background jobs correctly, but my laptops will.

If I run the scripts in the previous problem on my laptops, they work perfectly. Script A starts Script B as a background job, and Script B calls Script C from within that job, and all output is received. If I run the exact same scripts on my server, Script A calls Script B, and Script B hangs indefinitely. If I run Script B directly, it executes perfectly on the server or the laptops. It's something about the job being backgrounded that's killing me.

$PSVersionTable returns the same results on all computers, though obvious I had to use different installers on WinXP vs Win2003R2.

What might be causing the difference in behaviour?

Is there any way to troubleshoot what's going on in the background jobs? If I could see the command lines being received (I've logged what's being sent, but sometimes things drift), or what object is really hanging things, maybe it would help, but the debugger won't take me there. Perhaps there's a way to call the job within the ISE as if it were running in the background?


回答1:


Ah. I think maybe I have my answer.

Laptop:

PS Z:\jobs> winrm quickconfig
WinRM already is set up to receive requests on this machine.
WinRM already is set up for remote management on this machine.

Server:

PS D:\jobs> winrm quickconfig
WinRM already is set up to receive requests on this machine.
WinRM is not set up to allow remote access to this machine for management.
The following changes must be made:

Create a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.

If anyone can confirm this or give info on troubleshooting background jobs, I'll mark the question answered.




回答2:


You can get the output result of a Powershell job by using Receive Job. For instance, if we had a job like as follows:

$myJob = start-job -ScriptBlock  { 
    Write-Output "Some stuff happening..."
    throw "Hello World!"
}

We could get the output of the job like this:

Receive-Job $myJob    #prints the message and exception


来源:https://stackoverflow.com/questions/5196435/how-do-i-debug-a-powershell-background-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!