开发者

Powershell bug where external commands silently fail

I have a complex powershell script that uses remoting and does a lot of 开发者_StackOverflow社区file copying and loading and unloading of powershell modules. Within the script I use robocopy to move only changes files and anything works OK.

However, every now and then the powershell session gets into a weird state whereby it no longer executes external commands. For instance if I type robocopy into the console it just returns immediately with no error and no output. If I type whoami, it again returns immediately with no output. It only gets into this state about 1 in 10 executions of the same script.

If I close the console and start a new session everything goes back to normal. Any idea what's going in here? I can't figure out a way to debug or fix this state.

Thanks, Mike

Update It looks like there's a known issue with passing arrays to write-host that can cause this behavior. I don't believe I'm doing this, but it's difficult to know for sure. I've managed to put a trap in place testing when the result of whoami becomes empty. Oddly it appears to happen whilst waiting for a long running remote operation.


Unfortunately I don't have an answer for you, but I have also seen this exact behavior and I can add some insight; I know for a fact that I am not using write-host, and this problem only occurs when I use powershell V2's background jobs and remoting together. Typically this happens after I've run a fairly large number of background jobs, and just like you the problem is isolated to that single powershell process. Relaunching powershell always fixes the problem.


For anyone else that may run into this - check all lines that have write-host - in my case the culprit was this line in the script:

Write-Host **************************************

It was used at the end as a visual marker that all loops have completed and below it there was a summary... As soon as I remove it, the commands execute. If I add it, they stop executing... Maybe the asterisks overflow some buffer? Not sure but I will never use them again... :)


If you are using /R option with the robocopy command then try increasing the number of retries. Better use the default value. Hope this works fine.

-Archit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜