$FilePath = 'Z:\next\ResourcesConfiguration.config'
$oldString = 'Z:\next\Core\Resources\'
$NewString = 'G:\PublishDir\next\Core\Resources\'
任何想法如何替换具有:的字符串。我想更改配置文件中的路径。简单的代码对此不起作用。尝试以下
(Get-Content $original_file) | Foreach-Object {
$_ -replace $oldString, $NewString
} | Set-Content $destination_file
最佳答案
Replace运算符采用正则表达式模式,“\”在正则表达式中具有特殊含义,它是转义符。您需要将每个反斜杠加倍,或者最好使用转义方法:
$_ -replace [regex]::escape($oldString), $NewString
或者,您可以使用string.replace方法,该方法采用字符串并且不需要特别注意:
$_.Replace($oldString,$NewString)
关于string - 包括传递字符串:Powershell脚本中-Replace Variable的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492987/