# XML Code won't write correctly if not using an interactive connection?

**URL:** <https://forums.powershell.org/t/xml-code-wont-write-correctly-if-not-using-an-interactive-connection/24947>\
**Category:** PowerShell Help\
**Created:** [November 1, 2024, 12:28am UTC](https://forums.powershell.org/t/xml-code-wont-write-correctly-if-not-using-an-interactive-connection/24947 "2024-11-01T00:28:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![helpdeskaleer](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@helpdeskaleer](https://forums.powershell.org/u/helpdeskaleer)\
**Post date:** [November 1, 2024, 12:28am UTC](https://forums.powershell.org/t/xml-code-wont-write-correctly-if-not-using-an-interactive-connection/24947/1 "2024-11-01T00:28:04Z")

</div>

It appears to work on my desktop which is running Windows 10 21H2:

My unit tests all pass and I’ve compared the input and output files.

```auto
Starting discovery in 1 files.
Discovery found 4 tests in 1.8s.
Running tests.
[+] C:\proj\test\pwrsh\simph_reload-Test.ps1 8.61s (2.62s|4.28s)
Tests completed in 8.79s
Tests Passed: 4, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

```

But it does not work on the target machine which is running a very old Windows 10 1607.

I was attempting to pretty up the resulting file so it is indented and such.

```auto
class SWebConfigManager {

	[string]$webConfPath = ""
        # ...

	addElements() {
		Write-Information("SWebConfigManager.psm1: BEGIN addElements()")

		# Whatever... https://stackoverflow.com/a/48130111/18149

		$wc = New-Object System.xml.XmlDocument
		$wc.PreserveWhitespace = $true # Before loading like they said...
		$wc = [xml]([System.IO.File]::ReadAllText($this.webConfPath))
		
                # Classes for Pretty Printing with nice indentation.
		$settings = New-Object System.Xml.XmlWriterSettings
		$settings.Indent = $true
		$settings.NewLineChars="`r`n" # Match the old file.
		$settings.Encoding = New-Object System.Text.ASCIIEncoding # Match the original encoding, we checked.

		$addNodeOne = $wc.CreateElement("add");
		$addNodeTwo = $wc.CreateElement("add");
		$keyAttOne = $wc.CreateAttribute("key");
		$keyAttTwo = $wc.CreateAttribute("key");
		$keyAttOne.Value = "SomeOption1";
		$keyAttTwo.Value = "SomeOption2";
		$valueAttOne = $wc.CreateAttribute("value");
		$valueAttTwo = $wc.CreateAttribute("value");
		$valueAttOne.Value = "true";
		$valueAttTwo.Value = "true";

		$addNodeOne.Attributes.Append($keyAttOne);
		$addNodeTwo.Attributes.Append($keyAttTwo);

		$addNodeOne.Attributes.Append($valueAttOne);
		$addNodeTwo.Attributes.Append($valueAttTwo);

		$wc.configuration.appSettings.AppendChild($addNodeOne);
		$wc.configuration.appSettings.AppendChild($addNodeTwo);

		$xtw = [System.xml.XmlTextWriter]::Create("$($this.webConfPath)", $settings)
		try {
	         $wc.Save($xtw)
		} catch {
			Write-Error "$($_)"
		}
		finally {
		    $xtw.Dispose()
		}
		Write-Information("SWebConfigManager.psm1: END addElements()")
	}

# ...
}
# ...

```

I’m running as an administrator to the machine and using `Invoke-command` to run it after the module has been copied over…but the file is untouched once it runs.

If I use `Enter-PSSession` instead and run the same code it works.

Any ideas?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 1, 2024, 12:28am UTC](https://forums.powershell.org/t/xml-code-wont-write-correctly-if-not-using-an-interactive-connection/24947/2 "2024-12-01T00:28:31Z")

</div>

Closed on December 1, 2024, 12:28am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
