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
PowershellXML
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>

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
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 5 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 5 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004