We help IT Professionals succeed at work.

Unexpected results on PowerShell ForEach loop over XML child nodes

Alan Varga
Alan Varga asked
on
2,017 Views
Last Modified: 2017-04-24
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Alan VargaAccess Developer

Author

Commented:
Well, that was simple!

A leading single slash starts me out at the root; how/when would I use a leading double-slash?
Alan VargaAccess Developer

Author

Commented:
This was a prompt, simple and direct answer to a quick question.  Great job; thanks!
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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.
Alan VargaAccess Developer

Author

Commented:
Got it; thanks again.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.