Skip to content

XML nodes are too picky about setting values #2459

@Jaykul

Description

@Jaykul

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions