2

I have a PowerShell script that must take an xml file as the input source for all the parameters/variables that will be used in the script.

How I have the param xml input code currently:

param([xml] $xmlData)

But when the script is executed I get this error:

PS> .\script.ps1 -xmlData .\xmlfile.xml
script.ps1 : Cannot process argument transformation on parameter 'xmlData'. Cannot convert
value ".\xmlfile.xml" 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."
At line:1 char:22
+ .\script.ps1 -xmlData .\xmlfile.xml
+                      ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [script.ps1], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,script.ps1

In a PS session if I do this it works fine and I can see the nodes and data parsed from the xml file, so I'm not sure how this should be done correctly or if I'm missing something:

PS> $xml = [xml] (gc xmlfile.xml)

PS> $xml.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    XmlDocument                              System.Xml.XmlNode

As a last note, my xml file does contain the xml version tag and the structure is simple:

<root>
<value1>...
<value2>...
   <subnode>...
   <subvalue1>...

I'm skipping the closing tags and everything.

2 Answers 2

5

From your description, it sounds like you need to be calling your script like this:

.\script.ps1 -xmlData [xml](get-content .\xmlfile.xml)

It's expepcting an XML object as input, not a file path, so you need to do that transform before it's passed to the script.

Sign up to request clarification or add additional context in comments.

1 Comment

For me it worked in the following way- .\script.ps1 -xmlData ([xml](get-content .\xmlfile.xml))
1

Your script is expecting an XML object as the argument of the xmlData parameter. However, you are giving it the path to the XML file instead.

You need to call the script like this:

PS> .\script.ps1 -xmlData [xml](Get-Content .\xmlfile.xml)

In the above code, (Get-Content .\xmlfile.xml) gets the text in .\xmlfile.xml. [xml] then converts that text into an XML object, which is exactly what your script is expecting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.