Link to home
Start Free TrialLog in
Avatar of Alan Varga
Alan VargaFlag for United States of America

asked on

Unexpected results on PowerShell ForEach loop over XML child nodes

When I try to iterate through 6 child nodes, I get the data from the first node 6 times.  What do I need to change?

Here is my XML:
<MaintenanceConfiguration>
    <metadata>
        ...
    </metadata>

    <server name="whserver02" />

    <clients>
        ....
    </clients>

    <files>
        <file>
            <name>Backup Firefox Prefs (4.1 beta).vbs</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Backup Firefox Prefs\4-1-0 beta\Backup Firefox Prefs-program2</server_location>
            <client_location>C_Program2\Smith\Backup Firefox Prefs</client_location>
        </file>
        <file>
            <name>um.menuitems.xml</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Update Manager\4-0-1\Update Manager-app data\</server_location>
            <client_location>C_ProgramData_Smith\Update Manager</client_location>
        </file>
        <file>
            <name>vbsgeneral.include</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Common Files\1-5-15\</server_location>
            <client_location>C_Program2\Smith\Common Files</client_location>
        </file>
        <file>
            <name>computers.xml</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Common Files\1-5-15\</server_location>
            <client_location>C_Program2\Smith\Common Files</client_location>
        </file>
        <file>
            <name>Restore network mappings.cmd</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Network Mapping\5-0-0\</server_location>
            <client_location>C_Program2\Smith\Network Mapping</client_location>
        </file>
        <file>
            <name>RestoreNetworkMappings.ps1</name>
            <server_location>Public\Install Programs-32 bit\current\Smith\Network Mapping\5-0-0\</server_location>
            <client_location>C_Program2\Smith\Network Mapping</client_location>
        </file>
    </files>
</MaintenanceConfiguration>

Open in new window


and here is my loop:
$FilesNode = $ConfigurationFile.SelectSingleNode("/MaintenanceConfiguration/files")
ForEach ($FileNode in $FilesNode.ChildNodes) {
    $strName = $FileNode.SelectSingleNode("//name").InnerText
    $strSource = $FileNode.SelectSingleNode("//server_location").InnerText
    $strTarget = $FileNode.SelectSingleNode("//client_location").InnerText
    Write-Host $strName `n$strSource `n$strTarget `n -ForegroundColor Cyan
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alan Varga

ASKER

Well, that was simple!

A leading single slash starts me out at the root; how/when would I use a leading double-slash?
This was a prompt, simple and direct answer to a quick question.  Great job; thanks!
Avatar of oBdA
oBdA

The leading double slash searches from the root and all subnodes. The <name> nodes inside your <file> nodes just happen to be the first ones in the document. If there would have been a child node <name>, for example, under <server>, you would have found this one.
Got it; thanks again.