title: Powershell and XML file creation
body: <p>This is the first time I've ever tried to create an XML file. I am trying to write a script that queries our list of GPOs and generates an XML file that has the name of the name of the GPO and the servers to which the apply.</p>
  
  <p>I am running into two issues:</p>
  
  <ul>
  <li>The XML file is created with the first GPO, but I get all of the servers in "NodeName" tag, instead of many.</li>
  </ul>
  
  <blockquote>
    <p>&lt; MasterList ><br>
        &lt; Tag ><br>
        &lt; TagName >WSUS-ALPHA &lt; /TagName ><br>
        &lt; NodeName >SERVER1 SERVER2 SERVER3 SERVER4 &lt; /NodeName ><br>
        &lt; /Tag ><br>
        &lt; /MasterList ></p>
  </blockquote>
  
  <ul>
  <li>After the first GPO is written to the XML file, I get the following errors for the rest:</li>
  </ul>
  
  <blockquote>
    <p>Exception calling "WriteStartElement" with "1" argument(s): "The Writer is closed."<br>
    At line:3 char:5<br>
    + $xmlWriter.WriteStartElement('Tag')<br>
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
    + FullyQualifiedErrorId : InvalidOperationException</p>
    
    <p>Exception calling "WriteElementString" with "2" argument(s): "Token StartElement in state Error would result in an
    invalid XML document."
    At line:4 char:5
    +     $xmlWriter.WriteElementString('TagName',$gpo.DisplayName)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException</p>
    
    <p>Exception calling "WriteElementString" with "2" argument(s): "Token StartElement in state Error would result in an
    invalid XML document."<br>
    At line:10 char:13<br>
    +             $xmlWriter.WriteElementString('NodeName',(Get-ADGroupMember -Identit ...<br>
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
        + FullyQualifiedErrorId : InvalidOperationException</p>
    
    <p>Exception calling "WriteEndElement" with "0" argument(s): "There was no XML start tag open."<br>
    At line:13 char:5<br>
    +     $xmlWriter.WriteEndElement()<br>
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
        + FullyQualifiedErrorId : InvalidOperationException</p>
    
    <p>Exception calling "WriteEndElement" with "0" argument(s): "There was no XML start tag open."<br>
    At line:14 char:5<br>
    +     $xmlWriter.WriteEndElement()<br>
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
        + FullyQualifiedErrorId : InvalidOperationException</p>
    
    <p>Exception calling "Flush" with "0" argument(s): "Cannot write to a closed TextWriter."<br>
    At line:15 char:5<br>
    +     $xmlWriter.Flush()<br>
    +     ~~~~~~~~~~~~~~~~~~<br>
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
        + FullyQualifiedErrorId : ObjectDisposedException</p>
  </blockquote>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>$xmlFile = "c:\data-$randomnumber.xml"
  $xmlWriter = New-Object System.XMl.XmlTextWriter($xmlFile,$Null)
  $xmlWriter.Formatting = 'Indented'
  $xmlWriter.Indentation = 1
  $xmlWriter.IndentChar = "`t"
  $xmlWriter.WriteStartElement('MasterList')
  
  #Get the list of GPOs that start with "wsus".
  $wsusGPOs = Get-GPO -All | where {$_.DisplayName -like 'wsus*'} 
  
  Foreach ($gpo in $wsusGPOs) {
      #For each GPO that starts with "wsus", create a tag with the GPO's name.
      $xmlWriter.WriteStartElement('Tag')
      $xmlWriter.WriteElementString('TagName',$gpo.DisplayName)
  
      #Get the list of AD groups that have the "Apply" permission, in each GPO.
      $wsusPerms = $gpo | Get-GPPermission -All | where {$_.permission -eq 'GpoApply' -and $_.denied -eq $false}
      Foreach ($permsList in $wsusPerms) {
          If ($permsList.trustee.name -ne 'Authenticated Users') {
              #For each AD group that can apply the GPO, get the list of servers in the AD group. Ignores the "Authenticated Users" group. 
              $xmlWriter.WriteElementString('NodeName',(Get-ADGroupMember -Identity $permsList.Trustee.name -Recursive).name)
          }
      }
  
      $xmlWriter.WriteEndElement()
      $xmlWriter.WriteEndElement()
      $xmlWriter.Flush()
      $xmlWriter.Close()
  
      }
  </code></pre>
  
  <p>I tried moving the $xmlWriter.Flush() and $xmlWriter.Close() commands out of the top foreach loop, but that didn't help.</p>
  
  <p>I feel like both of these issues should be fairly straight-forward to address, I'm just not sure how. Any thoughts on how I should modify my code?</p>
  
  <p>Thanks.</p>