我有 powershell 文件,其中我有如下变量 decalration 行
[string] $global:myExePath = "\\myshare\code\scripts";
Run Code Online (Sandbox Code Playgroud)
我想更换 \\myshare\code\scripts
与\\mynewshare\code1\psscript
在运行时通过执行PowerShell脚本。
我在用
Get-Content $originalfile | ForEach-Object { $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName } | Set-Content ($originalfile)
如果我正在执行
{ $_ -replace "scripts", $mynewcodelocation.FullName }
它工作正常,但它不工作{ $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName }
这里有什么问题?
'\' 是一种特殊的正则表达式字符,用于转义其他特殊字符。您需要将每个反斜杠加倍以匹配一个反斜杠。
-replace "\\\\myshare\\code\\scripts",$mynewcodelocation.FullName
Run Code Online (Sandbox Code Playgroud)
当您不知道字符串的内容时,您可以使用转义方法为您转义字符串:
$unc = [regex]::escape("\\myshare\code\scripts")
$unc
\\\\myshare\\code\\scripts
-replace $unc,$mynewcodelocation.FullName
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6148 次 |
最近记录: |