0

I have a xml snippet like the one below:

<userSettings>
  <setting name="DataLimit">500</setting>
  <setting name="ChunkSize">7000</setting>
  <setting name="RequestFormat">rawXML</setting>

I want to update the xml cell value of ChunkSize from 7000 to 500000 and have the following script which I picked up from Stack Overflow:

$xml = [xml](Get-Content "C:\Users\ldap_user\AppData\Local\Office Connection\OfficeReportingSettings.xml")
$xml.SelectNodes("//ChunkSize") | % { 
    $_."#text" = $_."#text".Replace("7000", "5000000") 
    }

$xml.Save("C:\Users\ldap_user\AppData\Local\Office Connection\OfficeReportingSettings.xml")

The script doesn't error out and I see the .xml file timestamp changed to current time. However the value still stays 7000. Please let me know what am I missing. Also I want to run this against the same .xml file residing in hundreds of user profile on the same terminal server. BTW I am running this script on a Windows 2016 server and the version of Powershell is:

(Get-Host).Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  3383    

Thanks a lot in advance

4
  • 1
    Set your node selection to be $xml.SelectNodes("//setting[@name='ChunkSize']"). Commented Feb 21, 2020 at 19:54
  • 1
    Adding on to @AdminOfThings's advice, the steps for debugging this would be to confirm that $xml.SelectNodes("//ChunkSize") produces what you expect (it won't), then $_."#text", then $_."#text".Replace("7000", "5000000"). Commented Feb 21, 2020 at 19:58
  • @AdminOfThings, Thank you so much. I get this error: Exception calling "SelectNodes" with "1" argument(s): "'//setting[@name='ChunkSize')]' has an invalid token." At D:\Utils\Alter_xml_Chunk_Set.ps1:2 char:1 + $xml.SelectNodes("//setting[@name='ChunkSize')]") | % { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException Commented Feb 21, 2020 at 20:08
  • @user3605317 your error message shows an unfavorable mix of quote pairs. You should use double quotes around the whole XPath and single quotes around ChunkSize. Commented Feb 21, 2020 at 20:13

1 Answer 1

1

You are having issues with saving your updated content because you are not actually making any changes to the content. Your node selection syntax must change so that you are actually selecting an applicable element and attribute.

$xml.SelectNodes("//setting[@name='ChunkSize']") | Foreach-Object { 
    $_.'#text' = $_.'#text'.Replace('7000','5000000') 
}

The XPath syntax breaks down to //element and [@attribute='value']. setting is the element. name is the attribute. ChunkSize is the attribute value. Keep in mind that XPath queries are case sensitive.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please provide me a hint as to how to alter this file residing in multiple User profiles? The file resides under: C:\Users\ldap_user1\AppData\Local\Office Connection, C:\Users\ldap_user2\AppData\Local\Office Connection and so on... Not a Powershell expert, if you can help as to how to loop through User's profiles, that would be helpful. Thanks
@user3605317, I would loop through your directories with a Get-ChildItem -Path C:\Users -Directory | foreach-object { $path = Join-Path -Path $_.FullName -ChildPath 'AppData\Local\Office Connection\OfficeReportingSettings.xml'; $xml = [xml](Get-Content $path); $xml.Save($path) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.