替换<title>欢迎</title>将不起作用

时间:2014-05-19 16:39:15

标签: html powershell replace

我有一个aspx文件我尝试使用powershell 1.0进行更新。该文件看起来像:

<html>
 <head>
  <title>Welcome!</title>

我用:

(Get-Content "C:\documents and settings\login.aspx") | ForEach-Object{$_ -replace "<title>Welcome!</title>","<title>Welcome! Warning...</title>"} | Set-Content "C:\documents and settings\login.aspx"

它什么也没做。

但是,我使用:

(Get-Content "C:\documents and settings\login.aspx") | ForEach-Object{$_ -replace "<title>Welcome!","<title>Welcome! Warning..."} | Set-Content "C:\documents and settings\login.aspx"

它取代了它,但显然是错误的方式:

<html>
 <head>
  <title>Welcome! Warning...</title></title>

这里发生了什么?我希望我的第一个例子能够运作

2 个答案:

答案 0 :(得分:0)

Get-Content "C:\documents and settings\login.aspx" |

正在读取每行文件并立即将其传递给管道...文件正在使用中,但您正在尝试更改最后一个cmdlet中的文件=&gt;没有好处

(Get-Content "C:\documents and settings\login.aspx") |

您正在读取该文件,并在完成该操作后,然后将每一行传递给管道,这很好,您可以更新该文件,因为它没有被使用

答案 1 :(得分:0)

我弄清楚了我的错误。我的原始脚本中有一个拼写错误(此处未显示)。为了隐私目的,我在这个问题的类似代码中写了我实际工作的内容。在为这个项目工作一天后,我回到它并看到了错字。我纠正了它现在有效。

谢谢!