0

i'm trying to get a string out of a file and want this string to be inserted/replaced in another file with Powershell commands

file1: "buildnumber": "[it's a value that contains the last commit id]" file2: "buildnumber": "xxxxxxxx"

File 1 (app.config.json) come direct from the pipeline with the last commit id, File2 (app.config.json_pipeline) is a template for the testers, but file2 should also contain current "buildnumber"

Further i want to embed this Powershell command into a batch script routine. as of this i used to execute

this is my statement, but it doesn't change the value in the app.config.json

powershell -Command "(Get-Content C:\[path_to_file]\app.config.json).replace('Select-String -Pattern "BuildNumber*"', '(Get-Content "C:\[path_to_file]\app.config.json_pipeline" | Select-String -Pattern "BuildNumber*")') | Set-Content C:\[path_to_file]\app.config.json"

Maybe somebody has an idea, i got a little stuck with this.

i'm afraid that i chose the wrong way the retrieve that needed value.

Don't really know i PS "replace" can handle "select-String" as value handler.

I want to keep it easy best as one line statement.

Many thanks in advance

i want to keep it easy, but don't know if my approach is the right one. i use get-content.replace | set-content very often to replace static values, but with this every changing value i'm not getting further

Compo
  • 36,585
  • 5
  • 27
  • 39
  • What you should do is open a PowerShell Prompt and determine how to perform the task there, before trying to do ot from a batch file. I have therefore removed your [[tag:batch-file]] tag, for now, because it is not yet relevant to your issue. Once you've got the correct powershell command line working, you can then update your question to show that, include your command to run it from a batch file, and reattach the tag. – Compo Nov 09 '22 at 10:19
  • 1
    General advise: don't peek and poke into **structured** text using text/string commands but use the intended parsers (like [`ConvertFrom/To-Json`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json)) instead. – iRon Nov 09 '22 at 11:03

0 Answers0