Replacement with new line creation

YaroC 311 Reputation points
2023-11-14T15:25:17.38+00:00

I have a config file with a number of lines where I'd need to insert a new line after certain text and also add text in another part of the config.

An example config:

property1=some_values;

property2=another_value;

property3='123456....';

property4="host1.com,host2.com";

Trying to add property3a in new line after property3 and add host3.com to property4. What I've got so far, which partially works, is the below code. Although the second replace works fine I'm getting the first one wrong. Rather than adding a new line with property 3a it completely replaces property3

foreach($hostname in $servers){
	$confPath = \\hostname\c$\some.conf
	$conf = Get-Content $confPath
	$conf | ForEach-Object {
				$_ `
				-replace '(property3.*)',("$1`nproperty3a=`"yet_another-value`";")`
				-replace 'property4=(.*)',('property4="$1,host4.com"')

			} | Set-Content $filePath

}
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,219 questions
{count} votes