0

my first post so hope this goes well! :)

I'm trying to use powershell to search through a file for a pattern and replace the entire line if it matches that pattern.

In the target file there are 2 instances of the pattern. The code below only works if there is one instance of the pattern 'jmsPassword':

$file='C:\Temp\ClientConduit.xml'

$line=Select-String $file -pattern "jmsPassword"
$line=$line.linenumber

$content = Get-Content $file
$content|
ForEach-Object {
    if ($_.ReadCount -eq $line) {
        $_ -replace '^.+','           <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>'
   } else {
    $_
   }
  } | Set-Content $file

Any ideas how I can get all instances replaced?

A sample section of text that needs editing looks as follows:

         <TECHNICIAN class="ParameterTechnician" compressvalets="N" encryptvalets="N" export="Y" name="ParameterTechnician" package="com.extendyourstore.foundation.manager.parameter" singleton="N">
       <PROPERTY propname="configScript" proptype="STRING" propvalue="classpath://config/manager/PosParameterTechnician.xml"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/parameters"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>

      <!-- comment out following variables if using JBoss -->
       <PROPERTY propname="clientID" proptype="STRING" propvalue="REG103"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="MDAxMaiyktgunPc0NKx7QAe3g"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/files"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="MDAxMaityhdxdfc0NKx7QAe3g"/>
       <PROPERTY propname="durableSubscriber" proptype="STRING" propvalue="Y"/>

The resulting affected lines should end up like this:

         <TECHNICIAN class="ParameterTechnician" compressvalets="N" encryptvalets="N" export="Y" name="ParameterTechnician" package="com.extendyourstore.foundation.manager.parameter" singleton="N">
       <PROPERTY propname="configScript" proptype="STRING" propvalue="classpath://config/manager/PosParameterTechnician.xml"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/parameters"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>

      <!-- comment out following variables if using JBoss -->
       <PROPERTY propname="clientID" proptype="STRING" propvalue="REG103"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/files"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>
       <PROPERTY propname="durableSubscriber" proptype="STRING" propvalue="Y"/>

Thanks!

1 Answer 1

1
$file='C:\Temp\ClientConduit.xml'
$replace='           <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>'
(Get-Content $file) | % {
    if($_ -like "*jmsPassword*") {
        $_ -replace '^.+',$replace
    }
    else {
        $_
    }
} | set-content $file
Sign up to request clarification or add additional context in comments.

3 Comments

Actually spoke to soon! :( Tried it on a live file and looks like the lines which contain a '+' sign do not get replaced e.g.: <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="MDAxMOsa37ol+C+pSwia13U44="/> Any Ideas @Cole9350 ?
Ok, managed to get it working by changing the following: if($_ -like 'jmsPassword') { $_ -replace '^.+',$replace :)
Yea the wildcard doesn't like that + sign for some reason, I'm not sure why. Updated answer

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.