0

need a bit of help here with multi line replacement and wildcards.

Long story short, I am writing a script to update our systems from Tomcat 7 to Tomcat 9, however there are some significant server.xml changes needed and the script needs to be written to work with wildcards to replace the site.bin name in the connector info.

Current script saves old Tomcat 7 server.xml file and relevant part is below: `

<Connector 
                   port="8443" maxThreads="150"
                   scheme="https" secure="true" SSLEnabled="true"
                   enableLookups="true" disableUploadTimeout="true"
                   keystoreFile="/webapps/site.bin" keystorePass="password"
                   clientAuth="false" sslProtocol="TLS"/>

`

New Tomcat 9 server xml below that needs to be replaced with the above:

`

<Connector port="8443" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

` I tried using the below code setting $oldCode as an array of the tomcat 9 connector info and $newCode as an array of the Tomcat 7 connector info, but it's not making a single change so I assume it's not finding that string in the replace command:

    $oldCode = @"
            <Connector port="8443" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
"@

$NewCode = @"
            <Connector 
                   port="8443" maxThreads="150"
                   scheme="https" secure="true" SSLEnabled="true"
                   enableLookups="true" disableUploadTimeout="true"
                   keystoreFile="/webapps/site.bin" keystorePass="password"
                   clientAuth="false" sslProtocol="TLS"/>
"@

(get-content -raw server.xml) -replace $oldCode, $NewCode | set-content Server-New.xml

What is the best way to replace the multi line array while accounting for variations in the "site.bin" syntax that will change from system to system?

Really lost on this one as I find it easy to replace single lines, or specific lines, but multiple lines with wildcards and specific xml formatting seems above my experience level at this time.

tsitalon1
  • 1
  • 1
  • 1
    Welcome to stackoverflow. What have you tried so far? Please read [ask] and edit your question with a starting point - as it is, this question is asking to do your work for you - but the problem space is incomplete, so it couldn't be done anyway: What needs to replaced by what? How did you do it in the past? etc. – Olaf Kock Nov 09 '22 at 17:23

1 Answers1

0

I'm not your authority in powershell, but you can go in two steps: first replace the whole block, but use a placeholder for site.bin

In the second step, just replace the placeholder with the actual target value for your target system

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Yes sir, I'm finding it difficult replacing the entire block. I've used XML parsing and string replacement before, but this particular file is formatted in a way making it difficult for me to parse. Most items appear to be in the InnerXml node. – tsitalon1 Nov 10 '22 at 12:46
  • Oh, I thought you had this replacement already from your old system and just need to adopt it from fewer to more lines (for the first step). In that case I misunderstood. How about bringing your own adapted server.xml (with placeholders), and just do a replacement of individual placeholders? – Olaf Kock Nov 10 '22 at 13:17
  • Found my answer by following this post, thanks again ! https://stackoverflow.com/questions/50123334/using-variables-inside-powershell-replace – tsitalon1 Nov 10 '22 at 16:39