PowerShell – Cannot process argument transformation on parameter ‘VARName’. Cannot convert value “System.Xml.XmlElement” to type “System.Xml.XmlDocument”. Error: “The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type

During one of my adventures into the world of PowerShell I came across the following error:

Cannot process argument transformation on parameter ‘Environment’. Cannot convert value “System.Xml.XmlElement” to type “System.Xml.XmlDocument”. Error: “The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.”

First I’ll explain what I’m trying to do.

I’ve got a PowerShell function that takes xml as a parameter

function Get-XmlData

{

param(
[xml]$XmlData
)

begin

{

$name = XmlData.Name

}

process

{

}

end

{

}

 

}

 

Then I’m calling the above function

[xml]$somexml = …

Get-XmlData -XmlData $somexml

This is where the above error appears.

Solution

I came across a solution here:

http://stackoverflow.com/questions/18040841/passing-an-xmlelement-to-a-background-job-in-powershell

After a bit of trying out I found an easier solution:

In my function I changed my Parameter to the type of string

   param(
[string]$XmlData
)

and then within my function I convert the string into Xml.

[xml]$XmlDataXml = $XmlData

 

 

Advertisements


Categories:PowerShell

Tags: ,

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: