I want to convert retrieve the info in xml tags using powershell. But this error message is what I am facing:
Select-Xml : Cannot convert value "System.Xml.XmlDocument" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."
[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)}
I want to retrieve the ID's from the computer resource. for that again,
foreach($str in $result)
{
$res = $str.split('/')[-1]
# next steps
}
I have searched a lot but not a thing is working.
Thanks for your help in advance.
-Xmlinstead of-Content. Second, the nodes are actually attributes of an element, not nodes named "Computer Resource", so the XPath would be//Computer/@Resource. And last but not least, usingSelect-Xmlfor this is unnecessarily complex, since you can just use the properties PowerShell magics up for XML documents and collections:$xml.BESAPI.Computer.ResourceDSW12SL02occurs nowhere in that document. You could ask a new question if necessary.