Powershell:使用foreach-object的xml中的特殊字符不能产生匹配

时间:2017-03-26 00:57:13

标签: xml powershell

我是初学者,并在此网页上找到了一个脚本,该脚本可以完成我的工作,但是当有特殊字符*'时,powershell无法生成我的CSV文件的匹配和第2列没有添加到XML文件。

这是脚本

$c = Import-Csv C:\temp\testlinks.csv
Get-ChildItem C:\temp\output.xml | Foreach-Object {
  # Out-String in the next line lets you work with a single string rather than an
  # array of strings.
  $xmldoc = (Get-Content $_.PSPath | Out-String)

  # Loop through your CSV, updating $xmldoc with each pass
  $c | Foreach-Object {
    $xmldoc = $xmldoc -replace $_.column1name, ($_.column1name + " " + $_.column2name)
  }

  # Write the updated $xmldoc back to the original XML file's path
  $xmldoc | Set-Content $_.PSPath
}  

这是我的CSV文件在column1中的内容,而我的XML文件包含:

<title lang="en">M*A*S*H</title>
<title lang="en">Who&apos;s The Boss?</title>

谢谢

1 个答案:

答案 0 :(得分:2)

-replace使用正则表达式来表示模式,因此您需要转义特殊字符。尝试:

$xmldoc = $xmldoc -replace ([regex]::Escape($_.column1name)), ($_.column1name + " " + $_.column2name)