Search code examples
xmlpowershell

Get node names based on node values


I have following XML

<?xml version="1.0" encoding="UTF-8"?>
<Configuration APIVersion="1905.1" IPS_CAT_VER="0">
    <Notificationlist transactionid="">
        <SendEmail>Enable</SendEmail>
        <SendSnmp>Disable</SendSnmp>
        <SignInEmail>Enable</SignInEmail>
        <SignInSnmp>Disable</SignInSnmp>
        <TooManyLoginEmail>Disable</TooManyLoginEmail>
        <TooManyLoginSnmp>Disable</TooManyLoginSnmp>
    </Notificationlist>
</Configuration>

I need two arrays, one with Enable Notificationlist and one with Disable Notificationlist like

Enable        Disable
------        -------
SendEmail     SendSnmp
SignInEmail   SignInSnmp
              TooManyLoginEmail
              TooManyLoginSnmp

I have tried like

$xml = (Select-Xml -Path C:\Users\Lenovo\Desktop\shellscripts\xml\SNMP.xml -XPath '/Configuration/Notificationlist')
$nodes = $xml | ForEach-Object { $_.Node | select -ExpandProperty InnerText }

but it only gives node values but not node name.


Solution

  • You can use below code, which will give you two variables $Enabled and $Disabled. To give you two arrays for node names for enabled and disabled values:

    $Enabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Enable") { $_ } }
    
    $Disabled = ($xml.Node | GM -MemberType Property | Select Name).Name | %{ if($xml.Node[$_].InnerText -eq "Disable") { $_ } }