简体   繁体   English

使用 Powershell 在 Invoke-CimMethod 中将 XML 文件作为输入参数传递

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

I am invoking a CIM method using power shell.我正在使用 Power Shell 调用 CIM 方法。 Method arguments are in XML file like this方法参数在这样的 XML 文件中

XML input file:- XML 输入文件:-

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

Powershell command:- Powershell 命令:-

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.当我像 @{field1='value1';field2='value2'} 这样的命令行参数传递时,它工作正常。

Is there any way to directly pass it as xml input file in power shell like we have '-file:input.xml' in winrm?.有什么方法可以像我们在 winrm 中使用“-file:input.xml”那样在 power shell 中直接将其作为 xml 输入文件传递?

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 文档并将其转换为 PowerShell 对象:

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

I think you could then expand Root in to its own object via:我认为你可以通过以下方式将Root扩展到它自己的对象:

$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).另外仅供参考,您的 XML 示例无效(field1 和 2 的结束元素在错​​误的位置有一个</ ,我希望这只是一个错字)。 It needs to be:它必须是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM