title: Powershell XML - Iterating Through Parent Nodes
body: <p>I am writing a Powershell Script which interrogates Active Directory based on an XML configuration file that looks something like this:</p>
  
  <pre><code>&lt;domains&gt;
      &lt;domain name="DOMAIN.INTERNAL" exclude="false"&gt;
          &lt;orgunit name="OU1" exclude="false"/&gt;
          &lt;orgunit name="OU2" exclude="false"&gt;
              &lt;orgunit name="OU3" exclude="false"/&gt;
              &lt;orgunit name="OU4" exclude="false"/&gt;
              &lt;orgunit name="OU5" exclude="true"/&gt;
          &lt;/orgunit&gt;
          &lt;host name="HOST1" exclude="false"/&gt;
          &lt;host name="HOST2" exclude="true" /&gt;
          &lt;host name="HOST3" exclude="true" /&gt;
      &lt;/domain&gt;
      &lt;domain name="SUB.DOMAIN.INTERNAL" exclude="false"&gt;
          &lt;orgunit name="OU6" exclude="false"&gt;
              &lt;orgunit name="OU7" exclude="false"&gt;
                  &lt;orgunit name="OU8" exclude="false"&gt;
                      &lt;host name="HOST4" exclude="false" /&gt;
                  &lt;/orgunit&gt;
              &lt;/orgunit&gt;
          &lt;/orgunit&gt;
          &lt;host name="HOST5" exclude="false"/&gt;
          &lt;orgunit name="OU7" exclude="true" /&gt;
      &lt;/domain&gt;
  &lt;/domains&gt;
  </code></pre>
  
  <p>I am loading the xml file, setting the xpath and then selecting the orgunit nodes that don't have children:</p>
  
  <pre><code>$currentPath=Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
  [xml]$configFile = Get-Content "$currentPath\WindowsUpdateOMatic.xml"
  
  foreach ($domain in $configFile.domains.domain) {
  
      $xpath = ("/domains/domain[@name=`"") + ($domain.name) + ("`"]//orgunit[@exclude=`"false`"][not (*)]/@name")
  
      foreach($node in Select-Xml -Xpath $xpath $configFile){
  
          # Find parent nodes
          $node.Node.ParentNode
      }
  }
  </code></pre>
  
  <p>My plan is to iterate through the the parent org unit nodes so that I can create the full distinguished name of an OU, e.g. "OU=bar,OU=foo,DC=sub,DC=domain,DC=internal".</p>
  
  <p>Then I can query the OU in active directory and retrieve host machines within it.</p>
  
  <p>The trouble is, the value of $node.Node.ParentNode doesn't return anything. Trying various things I did get it to return "InputStream" but I can't reproduce that just now.</p>
  
  <p>I'm fairly new to Powershell, I appreciate it looks like I'm probably trying to run before I can walk.</p>