One of my application is generating below XML file.
<root>
<command name="Set">
<property name="PWR.WakeupOnLAN" value="6" errorcode="0x0"/>
</command>
<command name="Set">
</command>
<command name="biossettings">
<property name="task" value="Succeeded." errorcode="0x0"/>
</command>
</root>
I am interested in reading the value and errorcodes of "PWR.WakeupOnLAN" property name. Before posting here, I tried various things but couldn't find correct code for reading properties in powershell. Can any one help me with powershell code for this?
In PowerShell 2.0 you can solve this using the new Select-Xml cmdlet and an XPath expression:
[xml]$document = "<root><command name='Set'><property name='PWR.WakeupOnLAN' value='6' errorcode='0x0'/></command><command name='Set'></command><command name='biossettings'><property name='task' value='Succeeded.' errorcode='0x0'/></command>"
$value = (Select-Xml -Xpath "//property[@name='PWR.WakeupOnLAN']/@value" $document).Node.Value
$errorCode = (Select-Xml -Xpath "//property[@name='PWR.WakeupOnLAN']/@errorcode" $document).Node.Value
Related resources:
@Enrico Campidoglio gives the "cleanest" solution here is a kind of old fashion.
PS> $xml = [XML](get-content c:\temp\yourfile.xml)
PS> $errcode = ($xml.root.command | where {$_.property.name -eq "PWR.WakeupOnLAN" }).property.errorcode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With