首页 > 解决方案 > 使用 powershell 检索 xml 值

问题描述

我想使用 powershell 转换检索 xml 标签中的信息。但是这个错误消息是我所面临的:

Select-Xml:无法将值“System.Xml.XmlDocument”转换为类型“System.Xml.XmlDocument”。错误:“指定的节点不能作为该节点的有效子节点插入,因为指定的节点类型错误。”

[xml]$xml=
@"
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Computer Resource="https://dsw11.lab.g:52311/api/computer/549"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/550"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/552"/>
<Computer Resource="https://dsw11.lab.g:52311/api/computer/551"/>
</BESAPI> 
"@


#I have tried these alternatives for result, but both of them are not working

#$result = Select-Xml -Content $xml -XPath "//Computer Resource" | foreach {$_.node.InnerXML}
$result = Select-Xml -Content $xml -XPath "//Computer Resource" | foreach {[pscustomobject]@($_.node.InnerXML)}

我想从计算机资源中检索 ID。再次为此,

foreach($str in $result)
{
$res = $str.split('/')[-1]
# next steps
}

我搜索了很多,但没有任何工作。

提前感谢您的帮助。

标签: xmlpowershell

解决方案


Select-Xml -xml $xml -XPath "//Computer" | 
  foreach { $_.node.Resource.split('/')[-1] }

549
550
552
551

推荐阅读