简体   繁体   English

在powershell中的新行上添加内容

[英]add content on new line in powershell

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.我有一个向文件(.properties)添加内容的脚本,但下面的脚本在文件的最后一个字符后添加内容,但我想在新行添加内容。 I had tried `n but it create a blank line and then add content every time my add content command runs我试过 `n 但它创建一个空行,然后每次我的添加内容命令运行时添加内容

$Inputstring =@"
abc
def
ghi
"@

$list = @"
server_name1,server_name2,server_name3
"@
$lists = $list.split(",")
Foreach ($server in $lists) {   
    Add-content "path" $Inputstring
    
    
}

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.在这种情况下,您需要为每个服务器构建文件的 (UNC) 路径,然后在该文件上循环使用Add-Content

Suppose every server has that file in its local C:\\Temp\\TheFile.properties , then the code could be like this:假设每个服务器在其本地C:\\Temp\\TheFile.properties都有该文件,那么代码可能是这样的:

$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.最好有例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM