简体   繁体   English

Powershell 外部命令静默失败的错误

[英]Powershell bug where external commands silently fail

I have a complex powershell script that uses remoting and does a lot of file copying and loading and unloading of powershell modules.我有一个复杂的 powershell 脚本,它使用远程处理并执行大量文件复制以及 powershell 模块的加载和卸载。 Within the script I use robocopy to move only changes files and anything works OK.在脚本中,我使用 robocopy 仅移动更改文件,一切正常。

However, every now and then the powershell session gets into a weird state whereby it no longer executes external commands.然而,powershell session 时不时地进入一个奇怪的 state 从而不再执行外部命令。 For instance if I type robocopy into the console it just returns immediately with no error and no output.例如,如果我在控制台中键入 robocopy,它只会立即返回,没有错误,也没有 output。 If I type whoami, it again returns immediately with no output.如果我输入 whoami,它会再次立即返回,没有 output。 It only gets into this state about 1 in 10 executions of the same script.它只进入这个 state 大约十分之一的相同脚本执行。

If I close the console and start a new session everything goes back to normal.如果我关闭控制台并启动一个新的 session 一切都会恢复正常。 Any idea what's going in here?知道这里发生了什么吗? I can't figure out a way to debug or fix this state.我想不出调试或修复此 state 的方法。

Thanks, Mike谢谢,迈克

Update It looks like there's a known issue with passing arrays to write-host that can cause this behavior.更新将 arrays 传递给 write-host 似乎存在一个已知问题,可能导致此行为。 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.当 whoami 的结果为空时,我设法设置了一个陷阱测试。 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.我知道我没有使用 write-host,并且仅当我使用 powershell V2 的后台作业和远程处理时才会出现此问题。 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.通常,这发生在我运行了相当多的后台作业之后,就像您一样,问题与单个 powershell 进程隔离开来。 Relaunching powershell always fixes the problem.重新启动 powershell 始终可以解决问题。

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 的行 - 在我的情况下,罪魁祸首是脚本中的这一行:

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.如果您在 robocopy 命令中使用 /R 选项,请尝试增加重试次数。 Better use the default value.最好使用默认值。 Hope this works fine.希望这工作正常。

-Archit -建筑师

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM