为什么这不起作用?
$drvrInstFilePath = "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat"
echo $drvrInstFilePath
$drvrInstContent = Get-Content -LiteralPath "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat" | Out-String
echo 显示正确的路径,但 Get-Content 命令将 $oem 和 $1 扩展为空白字符串,即使它们已转义。为什么?
最佳答案
不要乱搞转义美元符号,而是使用单引号 '
而不是双引号 "
。它可以防止 PowerShell 将 $
扩展为变量。像这样,
$p = "C:\temp\Share\ISO$OEM$"
# Output
C:\temp\Share\ISO$
$p = 'C:\temp\Share\ISO$OEM$'
# Output
C:\temp\Share\ISO$OEM$
如果您需要使用变量创建路径,请考虑使用Join-Path
。就像这样,
$s = "Share"
join-path "C:\temp\$s" '\ISO$OEM$'
# Output
C:\temp\Share\ISO$OEM$
关于powershell - PowerShell 路径中的转义美元符号不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17452401/