【发布时间】:2017-12-29 04:55:59
【问题描述】:
我的原始配置文件 (web1.config) 没有多余的行,在记事本中查看时(显示所有字符)如下所示:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.6" />
</system.web>
<appSettings>
<add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</appSettings>
</configuration>
现在,我需要应用脚本将我的数据库名称更改为其他名称,如下所示:
Move-Item "web1.config" "webtemp.config"
Get-Content "webtemp.config" | ForEach-Object {$_ -replace "database=myDb;", "database=newDb;"} |Set-Content "web1.config" -Force
Remove-Item "webtemp.config"
Write-Output('Settings Changed')
因此,生成的新文件 (web1.config) 如下所示:
请注意在文件末尾添加的额外行(完全不需要) 我尝试了所有其他选项,例如: - 使用输出文件 api - 使用 .net IO 方法 System.IO.StreamWriter - 使用 -nonewline 标志(它将所有 10 行转换为单行) - 使用不同的编码选项 - 尝试将 \r\n 替换为 \r (不要再次工作,因为 set-content 总是会生成 crlf)
我正在使用 PowerShell v5.1。
【问题讨论】:
-
如果您的问题尚未得到完全解答,请考虑 accepting 提供答案或提供反馈。
标签: powershell newline powershell-5.0