本文介绍了PowerShell 的 Import-Clixml 从字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法运行Import-Clixml 字符串或 XML 对象上的 cmdlet?

Is there a way to run the Import-Clixml cmdlet on a string or XML object?

它需要一个文件路径作为输入来生成 PowerShell 对象,并且无法从 XML 对象获取输入.由于有 ConvertTo-Xml 将 PowerShell 对象序列化为 XML 对象的 cmdlet,为什么没有从 XML 进行的转换,这会做相反的事情?

It requires a file path as input to produce PowerShell objects and can't get input from an XML object. Since there is the ConvertTo-Xml cmdlet which serializes a PowerShell object into an XML object, why isn't there a convert from XML, which would do the opposite?

我知道 System.Xml.Serialization.XmlSerializer 类可以做到这一点.但是,我想坚持使用 cmdlet 来做到这一点.

I am aware of the System.Xml.Serialization.XmlSerializer class which would do just that. However, I would like to stick with cmdlets to do this.

有没有办法使用 cmdlet(可能只是使用 Import-Clixml)来做到这一点,而无需创建临时文件?

Is there a way to do this with cmdlets (probably just with Import-Clixml), without creating temporary files?

推荐答案

简短形式

[Management.Automation.PSSerializer]::Deserialize($cliXmlString)

长格式

概念证明

@{ a=1; b = 2,3,4; c = 3.14 } |  Export-Clixml -Path ~\Test.clixml
$cliXmlString = ( Get-Content -Path ~\Test.clixml ) -join ''
[Management.Automation.PSSerializer]::Deserialize($cliXmlString)

预期和观察到的输出

Name                           Value
----                           -----
c                              3.14
a                              1
b                              {2, 3, 4}

进一步阅读

PSSerializer.Deserialize(String) 方法

这篇关于PowerShell 的 Import-Clixml 从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 12:02