Why does Powershell’s “return” keyword cause type errors?

Question:


1: PowerShell functions return behavior

Answer:

Got some confirmation from the PowerShell team on this one. This appears to be a bug in the XML adapter. If you look at the object that is spit out by foo2 in a debugger it is a PSObject. If you don’t use the return keyword and instead just output the string “foo2” then the function returns a string object.

The bug in the XML adapter is that it is not unwrapping the PSObject to get at the base object. As a result when it tries to assign the PSObject to $xml.node it fails. For now, as a workaround, you can manually unwrap the psobject like so (or just cast to [string]):

Source:

Why does Powershell’s “return” keyword cause type errors? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply