使用PowerShell替换文件中的文本

时间:2017-10-18 21:25:40

标签: powershell

我从网上下载XML文件并将其写入本地目录。在XML文件的末尾有一系列标记:

  </Product>
 </Products>
</Feed>

我想用另一个文件(包括标签)的内容替换这些标签。这是我正在运行的Powershell:

$BVProductFeed = Invoke-WebRequest -Uri "[xml web page]" -OutFile "[local directory]" -TimeoutSec 1800000
$content = [IO.File]::ReadAllText("[file to append]")

$mergeFile = [IO.File]::ReadAllText("[xml web page]") -replace ("</Product> </Products> </Feed>",$content) | Set-Content "[updated file]"

脚本运行正常,它下拉XML,打开$ content文件就好了,但是当它写入$mergeFile时,它看起来与最初下载的$BVProductFeed文件相同。我推断这是我的替换声明的问题。我是否需要在标签之间包含CRLF?

感谢。

1 个答案:

答案 0 :(得分:0)

是的,CRLF标签必须包含在测试中才能替换整个字符串。一旦我添加了这些,我就能够删除和替换文本。

相关问题