Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在PowerShell one liner中逃离空间_Windows_Powershell_Escaping_Quoting - Fatal编程技术网

Windows 如何在PowerShell one liner中逃离空间

Windows 如何在PowerShell one liner中逃离空间,windows,powershell,escaping,quoting,Windows,Powershell,Escaping,Quoting,我想使用powershell单行运行带有参数的exe,如下所示- powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -ArgumentList somefile.txt, Run" 它是有效的,但当我试图插入带有空格的完整路径时,它崩溃了 powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -Argum

我想使用powershell单行运行带有参数的exe,如下所示-

powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -ArgumentList somefile.txt, Run"
它是有效的,但当我试图插入带有空格的完整路径时,它崩溃了

powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -ArgumentList "C:\Program Files\somefile.txt", "Run" "
我试着用单引号或转义,但没有用

也许有人知道我做错了什么


p.s.从cmd.exe内部运行

最简单的解决方案是对嵌入的字符串使用单引号(
“…”

powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -ArgumentList 'C:\Program Files\somefile.txt', 'Run'"
由于将解释命令字符串的是PowerShell,因此它将嵌入的
“…”
字符串识别为字符串文本


如果您试图嵌入双引号字符串(
“…”
),您将面临两个障碍:

  • 首先,PowerShell的CLI需要
    \
    作为
    的转义字符(而在内部它是
    `
    ),因此您需要将整个
    “…”
    命令字符串中的
    转义为
    \”

  • 此外,由于a,您需要额外的一轮转义,这使得解决方案非常模糊和笨拙,因为您必须将每个嵌入的
    转义为
    \”`
    (!)

powershell -ExecutionPolicy Unrestricted "Start-Process -Path program.exe -ArgumentList \"`\"C:\Program Files\somefile.txt\"`\", \"`\"Run\"`\" "