xmlpowershellwinrmwsman

Pass XML file as input Arguments in Invoke-CimMethod using powershell


I am invoking a CIM method using power shell. Method arguments are in XML file like this

XML input file:-

<root>
<field1>value1</<field1>
<field2>value2</<field2>
</root>

Powershell command:-

Invoke-CimMethod -InputObject $inst -CimSession $session -MethodName method_name -Arguments @{field1='value1';field2='value2'}

When i am passing as command line argument like @{field1='value1';field2='value2'} it works fine.

Is there any way to directly pass it as xml input file in power shell like we have '-file:input.xml' in winrm?.


Solution

  • You can't pass the XML file directly to the command, but you could open the XML document and convert it to a PowerShell object first:

    [xml]$XML = Get-Content -Path YourFile.xml
    

    I think you could then expand Root in to its own object via:

    $Root = $XML | Select -ExpandProperty Root | Select Field1,Field2
    

    I think then you could convert it to a hashtable via:

    $RootHT = @{}
    $Root.psobject.properties | Foreach { $RootHT[$_.Name] = $_.Value }
    

    Then pass that hashtable to your command:

    Invoke-CimMethod -InputObject $inst -CimSession $session -MethodName method_name -Arguments $RootHT
    

    You could combine some of the above steps to make this shorter if you wish.

    Also FYI, your XML example was invalid (the closing element of field1 and 2 have a </ in the wrong place which I expect was just a typo). It needs to be:

    <root>
    <field1>value1</field1>
    <field2>value2</field2>
    </root>