• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

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?
0
ntr2def
Asked:
ntr2def
  • 6
  • 4
1 Solution
 
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
 
ntr2defAuthor Commented:
Didn't see the code snippet. Let me test
0
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 
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
 
LearnctxEngineerCommented:
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
 
ntr2defAuthor Commented:
Worked perfectly and i appreciate your explanation thank you for this
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now