0

I am attempting to edit a configuration file using a batch script. I looked around and I believe powershell is the way to go here. I have zero experience with powershell so I'm guessing that the syntax is what is causing me problems.

Here is what the file looks like now (This section is located in the middle of the file)

    <!--add key="MinNumCycles" value="25"/-->
    <!--add key="MaxNumCycles" value="40"/-->

Here's what I want it to look like

    <!--add key="MinNumCycles" value="25"/-->
    <!--add key="MaxNumCycles" value="40"/-->

    <!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->
    <add key="RerunMode" value="0"/>

Here's what I'm trying to do in my batch file that I need help with

SET pattern=<!--add key="MaxNumCycles" value="40"/-->
SET textToAdd1=<!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->
SET textToAdd2=<add key="RerunMode" value="0"/>
SET filename=Software.exe.config

powershell -Command "(gc %filename%) -replace "%pattern%", "$&`n`n%textToAdd1%"'n"%textToAdd2%" | sc %filename%"

3 Answers 3

2
$pattern='<!--add key="MaxNumCycles" value="40"/-->'
$textToAdd = $pattern + '

    <!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->
    <add key="RerunMode" value="0"/>'

$filename = "Software.exe.config"

([IO.File]::ReadAllText($filename).Replace($pattern,$textToAdd) | Set-Content $filename -Force

Here's how I would replicate the batch file in all Powershell, personally.

  1. The way your Powershell command did the replace will expect RegEx and your pattern would not match the way you expected it to. It would match it as if it were a RegEx pattern and not match the exact string like you had typed. If you use the .NET string method .Replace(), it only looks for exact strings.
  2. $textToAdd contains the fully formatted final result, including the string we were searching for (both the beginning and end result have the string, this allows us to keep it there) as well as the concatenated addition. Per your description, the string marker is in the middle of the log, so this will allow it to just make those updates and re-save the log in it's entirety.
Sign up to request clarification or add additional context in comments.

6 Comments

I get "Method invocation failed because [System.Object[]] doesn't contain a method named 'Replace'. How do you run this? I am just doing powershell.exe -ExecutionPolicy Bypass -File script.ps1
for the $filename, are you using the full path to the file you want to update, by chance? your call to run the script is good, the error being hit confirms it
I tested it on my machine and everything worked fine... what version of Powershell are you using? You can run $PSVersionTable to check
Im on version 2.0
Alright I figured it out. I changed (Get-Content $filename) to [IO.File]::ReadAllText$filename)
|
0

From the PowerShell command line, this would work (assuming your existing contents are in conf.bat):

$content = Get-Content -Path 'C:\path\to\Software.exe.config'
$content += "`r`n"
$content += '<!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->'
$content += '<add key="RerunMode" value="0"/>'
Set-Content -Value $content -Path 'C:\path\to\Software.exe.config'

You could save this as a script, and run it using:

powershell.exe -File script.ps1

5 Comments

-Path "conf.bat" should probably be -Path "software.exe.config" in both the Get-Content and Set-Content lines - or better still, make the file name a replaceable parameter.
Good call. Added.
Would this work in the middle of the file? I should have specified that the section I'm working with is not at the end of the file
This will take the existing content of the file, append new lines, and then overwrite the file. It doesn't care (so to speak) about a "middle". Please update your original question if this is what you require - and, please provide an example of what you require at the end :)
Updated. Sorry about that
0
SET $pattern= '<!--add key="MaxNumCycles" value="40"/-->'
SET $textToAdd1='<!--RerunMode: 1 write to DB, 2 write to DB and add to 
RUN export/-->'
SET $textToAdd2='<add key="RerunMode" value="0"/>'
SET $filename='Software.exe.config'

(Get-Content $filename) -replace pattern, '`n' $textToAdd1 '`n' $textToAdd2 | Set-Content $filename

Similar to Robin's answer just another script you could run. He beat me to the punch :)

1 Comment

This still doesn't work for me. I am running like this

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.