title: Editing XML content with PowerShell working locally but not via Invoke-Command
body: <p>I'm trying to script the removal of a XML node using PowerShell from a .xml file on a remote computer.</p>
  
  <p>My <code>Invoke-Command</code> scriptblock is:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$remoteResult = Invoke-Command -ComputerName myserver.com -ScriptBlock {
      Param($FolderName) 
  
      $xmlFilePath = "c:\foo\Folders.xml"
      $localLog="c:\foo\removefolder.txt"
  
      [XML]$XMLContent = Get-Content $xmlFilePath -Encoding UTF8
      Write-Output $FolderName | Out-File $localLog -Append
      Write-Output "Entering loop" | Out-File $localLog -Append
      foreach ($Folder in $XMLContent.Folders) {
          $XMLFolderName = $Folder.SelectSingleNode("//Name[.='$FolderName']")
          Write-Output "Folder = $folder" | Out-String | Out-File $localLog -Append
          Write-Output "XMLUserName = $XMLFolderName" | Out-File $localLog -Append
          if ($XMLFolderName.'#text' -eq $FolderName) {
              $XMLUserName.'#text' | Out-File $localLog -Append
              Write-Output "User entry found for $FolderName, removing." | Out-File $localLog -Append
              $XMLFolderName.ParentNode.ParentNode.RemoveChild($XMLFolderName.ParentNode)
          }
          Write-Output "Folder entry not found?" | Out-File $localLog -Append
      }
      Write-Output "End loop" | Out-File $localLog -Append
      $XMLContent.Save($xmlFilePath)
  } -ArgumentList $remfolder *&gt;&amp;1
  </code></pre>
  
  <p>I've added a load of <code>Out-File</code> statements to generate a log on the remote machine to see what the state of things are during the script execution. I can see the <code>$FolderName</code> is being passed correctly, then I get:</p>
  
  <pre>
  Entering loop
  Folder = System.Xml.XmlElement
  
  XMLUserName =
  User entry not found?
  End loop
  </pre>
  
  <p>If I get it to <code>Out-File</code> the <code>Get-Content</code> line it successfully reads the file and saves it to the log, so the .xml file is being read successfully.</p>
  
  <p>If I copy the above code to the remote server and run it directly from there without the <code>Invoke-Command</code> statement, and manually pass the value of <code>$FolderName</code>, it works fine and removes the XML node as expected.</p>
  
  <p>So I'm not sure what is happening differently when it's run remotely.</p>
  
  <p>Edit:
  As requested, here's an example of the XML format being accessed.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Folders&gt;
    &lt;Folder&gt;
      &lt;Name&gt;Folder1&lt;/Name&gt;
      &lt;Location&gt;\\server.com\share$\folder\Folder1&lt;/Location&gt;
      &lt;Limitations /&gt;
      &lt;Description&gt;Folder One&lt;/Description&gt;
      &lt;CustomField /&gt;
      &lt;UserPermissions /&gt;
      &lt;GroupPermissions&gt;
        &lt;Permission&gt;
          &lt;IdentityName&gt;Folder One&lt;/IdentityName&gt;
          &lt;ListRestriction&gt;
            &lt;Type&gt;Deny&lt;/Type&gt;
            &lt;Extensions /&gt;
          &lt;/ListRestriction&gt;
        &lt;/Permission&gt;
      &lt;/GroupPermissions&gt;
    &lt;/Folder&gt;
    &lt;Folder&gt;
      &lt;Name&gt;Folder2&lt;/Name&gt;
      &lt;Location&gt;\\server.com\share$\folder\Folder2&lt;/Location&gt;
      &lt;Limitations /&gt;
      &lt;Description&gt;Folder Two&lt;/Description&gt;
      &lt;CustomField /&gt;
      &lt;UserPermissions&gt;
        &lt;Permission&gt;
          &lt;IdentityName&gt;Folder Two&lt;/IdentityName&gt;
          &lt;ListRestriction&gt;
            &lt;Type&gt;Deny&lt;/Type&gt;
            &lt;Extensions /&gt;
          &lt;/ListRestriction&gt;
        &lt;/Permission&gt;
      &lt;/UserPermissions&gt;
      &lt;GroupPermissions /&gt;
    &lt;/Folder&gt;
  </code></pre>
  
  <p>So for instance in the script I'm assigning $FolderName as "Folder2". When run locally it successfully removed that entire Folder node, remotely it doesn't.</p>