Powershell 在命令中使用引号中的变量

Powershell using variable in quotes within a command

我有这个可以用(三重双引号不是错误,它完全像这样工作)-专注于 Start-Process 部分:

Start-Process powershell -Credential $cred -WorkingDirectory "$lettre_disque" -ArgumentList '-noprofile -command &{Start-Process """D:\WD Drive Unlock.exe""" -verb runas}'

现在我只是想用一个名为 $chemin_fichier 的变量替换字符串 D:\WD Drive Unlock.exe,其中包含完全相同的文本 (D:\WD Drive Unlock.exe)

我正在努力:

 """$chemin_fichier"""

 ""$chemin_fichier""

 "$chemin_fichier"

 """"$chemin_fichier""""

没有任何效果...

如何显示该变量?这个命令很混乱,正确的组合是什么?我必须逃避什么吗?

非常感谢您的帮助!

我会使用 splatting 将你的命令分解成多个部分:

$chemin_fichier = 'D:\WD Drive Unlock.exe'

$ArgList = @{
    FilePath = 'powershell'
    Credential = $cred
    WorkingDirectory = $lettre_disque
    ArgumentList = @(
        '-NoProfile'
        '-Command'
        "`"Start-Process -FilePath '$chemin_fichier' -Verb runas`""
    )
}
Start-Process @ArgList

无需将命令包装在脚本块中。

我使用了一个带有这样参数的命令,它会将 store:Schema="abcd" 替换为空字符串,将 " 转义为 """""" 很关键,你应该试一试,测试和修改以确保脚本工作:

powershell -Command "$varStr='store:Schema=""abcd""""'; $filePath='%~dp0SomeFolder\SomeFile.txt'; (gc $filePath) -replace $varStr, '' | Out-File $filePath"