When setting an XML node content, PowerShell doesn't correctly unwrap PSObject strings.
For instance: Join-Path obviously outputs strings, but it outputs them wrapped in PSObjects
Steps to reproduce
[xml]$x = "<root><path/></root>"
$x.root.path = Join-Path C:\ Windows
Expected behavior
$x.root.path is an XML Node with string content "C:\Windows"
Actual behavior
A SetValueException is thrown:
Cannot set "path" because only strings can be used as values to set XmlNode properties.
At line:2 char:1
+ $x.root.path = Join-Path C:\ Windows
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueException
+ FullyQualifiedErrorId : XmlNodeSetShouldBeAString
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.206
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.206
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Workaround
Either of the following would work, but should not be necessary:
$x.root.path = (Join-Path C:\ Windows).PSObject.BaseObject
$x.root.path = (Join-Path C:\ Windows).ToString()
When setting an XML node content, PowerShell doesn't correctly unwrap PSObject strings.
For instance:
Join-Pathobviously outputs strings, but it outputs them wrapped in PSObjectsSteps to reproduce
Expected behavior
$x.root.pathis an XML Node with string content "C:\Windows"Actual behavior
A
SetValueExceptionis thrown:Environment data
Workaround
Either of the following would work, but should not be necessary: