The puppet-chocolatey community module uses reflection to modify the current PowerShell host. However, it was a bit fragile, generated exceptions, and propagated them through PowerShell module 2.0.
While the fix was put in at https://github.com/chocolatey/puppet-chocolatey/commit/166ae968dd455a9cf94cf3d88050f9a973c449ce, this still raises an interesting issue for executing user code.
Our code should be a little more defensive / explanatory, when the call to EndInvoke over user code is executed. As it stood, the error message generated was a bit misleading:
Debug: STDERR: Exception calling "EndInvoke" with "1" argument(s): "You cannot call a method on a null-valued expression."
The module can do a better job of identifying the problem is with user code (which may involve parsing the exceptions, etc).
This might also be a good time to do a simple cleanup of the repetitive $ps.Commands.Clear() calls that showed up in https://github.com/puppetlabs/puppetlabs-powershell/commit/9b785f0c149d59b7a91f1efb48f0bc8235f031c9 - these seem unnecessary, and the commit only briefly mentions "command pollution" without any explanation.