1

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?.

Chinmay Hegde
  • 117
  • 3
  • 10

1 Answers1

1

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>
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • Thanks for the input!.root element 'root' and child elements(field1, field2) may change for different xml file. I would like to have generic code that parses xml file and creates poweshell object. XML structure remains same(single level), but child elements will be field1, field2, field3 etc. – Chinmay Hegde May 04 '17 at 16:48