0

I have an xml file where I need to search for the string "plugin_name1" and then change the value for that plugin from 1 to 0. I'm using Select-String in PowerShell to find the string and return the previous 2 lines. I'm having trouble figuring out how to change the value from 1 to 0 on the first 2 output line below.

Note...the string EnableX and EnableY appear throughout the file, so I need to search specifically on the string "plugin_name1".

Also, I need to make the modification on about 100 workstations. The rest of the file varies from station to station but these 4 lines should all the same.

Any suggestions?
Thanks!

  PS C:\> Select-String -Path "C:\file.xml" -Pattern "plugin_name1" -Context 2, 0

  C:\file.xml:95:<EnableX>1</EnableX>
  C:\file.xml:96:<EnableY>1</EnableY>
> C:\file.xml:97:<PluginName>plugin_name1</PluginName>
> C:\file.xml:98:<Name>plugin_name1</Name>
halfraf
  • 1
  • 1
  • 1
    Unless the world is coming to an end, you should never manipulate an XML file with string manipulations. Even then you should think twice because every time that someone manipulates an XML file directly the end of the world gets a little closer... – EBGreen Feb 23 '18 at 19:30
  • Ha, OK. Any other suggestions? – halfraf Feb 23 '18 at 19:57
  • To start with, `[xml]$xmlFile = Get-Content "C\:file.txt"`. This will import the file as a [**`System.Xml.XmlDocument`**](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx), with all the perks. – G42 Feb 23 '18 at 20:00
  • I see...quick note. I need to make the modification on about 100 workstations. The file varies from station to station. – halfraf Feb 23 '18 at 20:05
  • as @gms0ulman showed you, get the doc as an XMLDocument object then just change the value of whatever node you want to change. There are all kinds of examples on the web. – EBGreen Feb 23 '18 at 20:07
  • The file may vary, but whatever application is using the file must have some consistent way to access specific parts of the XML. That is the point of a structured data language like XML. You just need to figure that bit out. – EBGreen Feb 23 '18 at 20:08
  • Thanks, this is new to me. I'll take a look. – halfraf Feb 23 '18 at 20:08
  • Thanks all! gms0ulman suggestion got me going in the right direction. – halfraf Feb 23 '18 at 21:28
  • why you dont replace 'plugin_name1 ' by 'plugin_name0' with simple replace on text? – Esperento57 Feb 26 '18 at 06:55

0 Answers0