1

i have a script which add content to the file(.properties) but the below script add content after the last character of file but i want to add content on new line. I had tried `n but it create a blank line and then add content every time my add content command runs

$Inputstring =@"
abc
def
ghi
"@

$list = @"
server_name1,server_name2,server_name3
"@
$lists = $list.split(",")
Foreach ($server in $lists) {   
    Add-content "path" $Inputstring
    
    
}
6
  • What does $env:servers contain exactly? Jul 19, 2021 at 8:58
  • -Path should be the path to the .properties file and you need to specify the value it should write in there: foreach ($thing in $listOfThings) { Add-Content -Path 'X:\Path\To\The\.properties' -Value "rn$thing" }
    – Theo
    Jul 19, 2021 at 9:03
  • @MathiasR.Jessen My bad , i have edited the code. Jul 19, 2021 at 9:04
  • @Theo sorry , i have edited my code now previous one was a mistake Jul 19, 2021 at 9:05
  • @Theo the content is in $inputString and for each is use to loop in servers Jul 19, 2021 at 9:06

1 Answer 1

1

I ASSUME this question is about how to append a certain string to a file on various servers with an extra newline above it.

In that case you need to construct the (UNC) path to the file for each server and then use Add-Content on that file in a loop.

Suppose every server has that file in its local C:\Temp\TheFile.properties, then the code could be like this:

$Inputstring =@"
abc
def
ghi
"@

# string array of servers. Could also be done with
# $servers = 'server_name1,server_name2,server_name' -split ','
$servers = 'server_name1','server_name2','server_name'

foreach ($server in $servers) {
    # construct the UNC path to the file on each server
    $path = "\\$server\C$\Temp\TheFile.properties"
    Add-content -Path $path -Value "`r`n$Inputstring"
}

If my assumption about your intention is wrong, please edit your question and make clear what it is you want to do. Preferably with examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.