【发布时间】:2018-10-13 23:58:09
【问题描述】:
我有一个包含多个占位符的配置文件,我想使用 PowerShell 将其替换为其他一些值,但下面的代码无法将 %WebClientPath%\www 替换为 $WebClientPath 变量中包含的值。它有这个路径“C:\IWeb\Demo\Main\UIPresentation\WebApp”
其中%VirtualApplicationName% 被 IMWeb
我尝试从 %WebClientPath% 中删除 \www 并且它有效,但我希望 \www 也被替换。我认为这是因为反斜杠 ( \ )
[CmdletBinding()]
Param
(
[string]$PathofWebConfigFile="C:\Automate\Web.config" ,
[string]$VirtualApplicationName="IMWeb"
)
# Code to get WebClient Path from PhysicalPath attribute.
$WebClientPath=(get-webapplication IMWeb).PhysicalPath
(Get-Content -Path "$PathofWebConfigFile") | ForEach-Object {$_ -Replace "%VirtualApplicationName%","$VirtualApplicationName"} | Set-Content -Path "$PathofWebConfigFile"
(Get-Content -Path "$PathofWebConfigFile") | ForEach-Object {$_ -Replace "%WebClientPath%\www","$WebClientPath"} | Set-Content -Path "$PathofWebConfigFile"
【问题讨论】:
-
你试过转义“\”字符吗?
-
我确实尝试过类似的方法,但没有成功 (Get-Content -Path "$PathofWebConfigFile") | ForEach-Object {$_ -替换 "`\www",""} |设置内容-路径“$PathofWebConfigFile”
-
让我知道在这种情况下使用 RegEx Escape 是否有帮助?
标签: xml powershell powershell-2.0 powershell-3.0 powershell-4.0