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

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.

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.

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?

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