传递包含的字符串:在powershell脚本中签署-Replace Variable

sam*_*sam 1 string powershell replace literals

$FilePath = 'Z:\next\ResourcesConfiguration.config'
$oldString = 'Z:\next\Core\Resources\'
$NewString = 'G:\PublishDir\next\Core\Resources\'
Run Code Online (Sandbox Code Playgroud)

任何想法如何替换具有以下内容的字符串:登录.我想更改配置文件中的路径.简单的代码不适用于此.试过以下

(Get-Content $original_file) | Foreach-Object {
 $_ -replace $oldString, $NewString
 } | Set-Content $destination_file
Run Code Online (Sandbox Code Playgroud)

Sha*_*evy 5

Replace运算符采用正则表达式模式,'\'在正则表达式中具有特殊含义,它是转义字符.你需要加倍每个反斜杠,或者更好,使用转义方法:

$_ -replace [regex]::escape($oldString), $NewString
Run Code Online (Sandbox Code Playgroud)

或者,你可以使用string.replace方法,它接受一个字符串,不需要特别小心:

$_.Replace($oldString,$NewString)
Run Code Online (Sandbox Code Playgroud)