I'm retrieving a string of text from some XML data, and then trying to replace that string with something else. The code I'm using is:
$newstring = "mystring"
$bar = Get-Gpo -Name "My GPO"
$gpoid = $bar.Id.ToString()
$drivesxml = New-Object XML
$drivesxml.Load("\\mydomain.co.uk\sysvol\mydomain.co.uk\Policies\{gpoid}\User\Preferences\Drives\drives.xml")
$oldpath = $drivesxml.Drives.Drive.Properties.Path[0]
$oldlabel = $drivesxml.Drives.Drive.Properties.Label[0]
#1
[string]$newtemppath = $oldpath -replace "Oldstring$", "$newstring"
[string]$newtemplabel = $oldlabel -replace "^Oldstring", "$newstring"
#2
$drivesxml.Drives.Drive.Properties.Path[0] = $newtemppath
$drivesxml.Drives.Drive.Properties.Label[0] = $newtemplabel
#3
$drivesxml.Save("\\mydomain.co.uk\sysvol\mydomain.co.uk\Policies\{gpoid}\User\Preferences\Drives\drives.xml")
It retrieves the XML from sysvol fine, and at point #1 if I query $oldpath
and $oldlabel
they contain the expected text from the XML.
At #2 if I query $newtemppath
and $newtemplabel
the strings are returned as expected with the text amended so instances of "Oldstring" have been replaced by "mystring".
At #3 if I query $drivesxml.Drives.Drive.Properties.Path[0]
and $drivesxml.Drives.Drive.Properties.Label[0]
I'd expect them to return the same content as the $newtemppath
and $newtemplabel
variables, but instead they continue to return their original values.
After saving the XML if I query it again the content hasn't changed.
Can anyone see what I might be doing wrong with the assignment between #2 and #3?