在不使用双引号(“)的情况下从cmd启动PowerShell脚本

nbysray5  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(102)

我需要在不使用双引号"的情况下从cmd启动PowerShell命令
我现在的指挥部:

powershell.exe -noexit Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'

错误:

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我不能使用双引号的原因是因为我正在使用vboxmanage中的post-install-command=参数,该参数已经使用了双引号。

jtoj6r0c

jtoj6r0c1#

  • 最好将所有以Start-BitsTransfer开头的内容包括在(转义的)"..."中**,即,将双引号字符串传递给powershell.exe的(位置隐含的)-Command参数,这将阻止cmd.exe解释字符串的内容(潜在的cmd.exe环境变量引用除外,例如%OS%)。
  • 如果没有"...",就像您的情况一样,"..."字符串外部的&将被解释为cmd.exe元字符(语句分隔符),并且调用Break*('...'字符串只被PowerShell识别,而不被cmd.exe识别)。
  • 虽然您可以对所有cmd.exe元字符单独进行^转义,但将整个-Command参数包含在"..."中是更简单的解决方案。(此外,尽管这在实践中很少出现问题,但在没有"..."封闭的情况下会发生空格标准化,即在每次运行时将多个空格合并为一个空格)。
  • 由于您的整个powershell.exe命令本身嵌入在作为vboxmanage选项的一部分传递给vboxmanage"..."字符串中,因此您必须*转义"字符。是powershell.exe调用的一部分,即as \"

因此,在vboxmanage调用的上下文中,使用如下内容-注意外部的"..."和嵌入的内部\"...\"

vboxmanage ... post-install-command="powershell.exe -noexit \"Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'\""

相关问题