无法使用PowerShell替换XML中的URL

时间:2019-01-29 13:04:41

标签: xml powershell

我正在尝试替换XML中的URL。例如

http://sample.com/v2/some/action 

http://sample-v2.com/v2/some/action

XML示例:

<someroot>
    <serice>
        <action url="http://sample.com/v2/some/action" />
    </service>
</someroot>

当我运行此脚本时:

$path = "..."
$line = "http://sample.com/v2/some/action"
$replacement = "http://sample-v2.com/v2/some/action"
(Get-Content $path) | Foreach-Object {
    $_ -replace $line, $replacement
}  | Out-File ($path + ".replaced.xml")

它不起作用。 我在做什么错了?

更新:

实际代码:

foreach ($line in $inputLines) {
    $replacement = $line.Replace("sample.test", "sample-" + $suffix + ".test")

    Write-Host ("Current input line:", $line) -ForegroundColor Yellow
    Write-Host ("Will be replaced with:", $replacement) -ForegroundColor Yellow

    (Get-Content $path) | Foreach-Object { 
        $_.Replace($line, $replacement) 
    }  | Out-File ($path + ".replaced.xml")
}

0 个答案:

没有答案