troubleshooting Question

Unexpected results on PowerShell ForEach loop over XML child nodes

Avatar of Alan Varga
Alan VargaFlag for United States of America asked on
XMLPowershell
5 Comments1 Solution2072 ViewsLast Modified:
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>

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
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros