powershell script to edit text

I've always been able to edit a script to replace something with something else for example

$Files=get-childitem . server.xml -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace 'directory="logs"', 'directory="E:/logfiles/TomcatLogs/AccessLog"'} |
Set-Content $file.PSPath
}

Always works but today I came across replacing text that had special characters in such as:


$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = ${catalina.base}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}

When i run that it does nothing, edits nothing. Can anyone help?
LVL 1
ntr2defAsked:
Who is Participating?
 
LearnctxConnect With a Mentor EngineerCommented:
In that case you can use forward/reverse lookups to achieve this. Also because you're using regular expressions. You not only have to worry about PowerShell special characters but also RegEx special characters. For example:

. (period) represents a single character
* (aster/start) represents 0 or more repetitions of a pattern

So in your RegEx you will want to escape these characters as well as the PowerShell special characters $ and { }.

Try this and see how it goes. It should match your pattern and put the string on the end.

$Files=get-childitem . catalina.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) | 
Foreach-Object {$_ -replace "(?<=common\.loader=\$\{catalina\.base\}/lib,\$\{catalina\.base\}/lib/\*\.jar,\$\{catalina\.home\}/lib,\$\{catalina\.home\}/lib/\*\.jar)", ",E:/Environment/jotmlib/*.jar"} | 
Set-Content $file.PSPath
}

Open in new window

0
 
LearnctxEngineerCommented:
The problem is the $ and { } characters. Escape these characters in your regular expression so that they become literal characters.

$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = \$\{catalina.base\}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}

Open in new window

0
 
ntr2defAuthor Commented:
I got that but that's the original txt in the file which I'm trying to edit out.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
ntr2defAuthor Commented:
Didn't see the code snippet. Let me test
0
 
ntr2defAuthor Commented:
it didnt work, no change in the document after i ran it
0
 
LearnctxEngineerCommented:
I can't full replicate what you're doing. But I've added a text file with the contents:

Line 1
String Begin 'directory = ${catalina.base}/logs' String End
Line 3

And run the following.

$files = ,(Get-ChildItem $env:temp | ? {$_.name -eq "test.txt"})
foreach ($file in $files)
{
  $content = Get-Content $file.fullname
  $content = $content | ForEach-Object {$_ -replace "'directory = \$\{catalina.base\}/logs'", "directory = E:/logfiles/TomcatLogs"}
  Set-Content $file.fullname -Force -Value $content
}

Open in new window

This is working and putting out the expected output.
0
 
ntr2defAuthor Commented:
it does work for a one liner i tried this approach with multiple instances for exmaple

Line 1
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar

Line 2

line 3

$Files=get-childitem . catalina.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) | 
Foreach-Object {$_ -replace "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar", "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar,E:/Environment/jotmlib/*.jar"} | 
Set-Content $file.PSPath
}

Open in new window

0
 
LearnctxEngineerCommented:
OK so in that case a whole lot has to be done differently. Are catalina.base and catalina.home completely different values? If they are you will require multiple replace statements.
0
 
ntr2defAuthor Commented:
actually I'm trying to add:
,E:/Environment/jotmlib/*.jar

to the end of:

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
0
 
ntr2defAuthor Commented:
Worked perfectly and i appreciate your explanation thank you for this
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.