首页 > 解决方案 > Powershell命令查找替换没有给出任何错误,但没有替换

问题描述

我正在尝试做的事情:

使用 powershell 将 .config 文件内容读取为原始字符串并找到特定字符串并替换为所需的字符串

我要查找和替换的行:

<add key="ReportServerUrl" value="http://SQL14\SQL2016_INS1/ReportServer"></add>

将上面的行替换为:

<add key="ReportServerUrl" value="http://SQL14/SQL2016_INS1/ReportServer"></add>

我试过的Powershell命令:

(Get-Content "C:\target\Server.config" -Raw).Replace("SQL14\\SQL2016_INS1/ReportServer","SQL14/SQL2016_INS1/ReportServer") | Set-Content C:\target\Server.config;

问题:

上面的语句执行没有任何错误,但上面没有按预期替换字符串

请帮忙

更新1:

尝试更换整线

 (Get-Content "C:\target\Server.config" -Raw).Replace('<add key="ReportServerUrl" value="http://SQL14\SQL2016_INS1/ReportServer"></add>
','<add key="ReportServerUrl" value="http://SQL14/SQL2016_INS1/ReportServer"></add>
') | Set-Content C:\target\Server.config;

这也可以在没有错误的情况下执行,但没有完成替换。

标签: powershellpowershell-3.0

解决方案


推荐阅读