I need to launch a powershell command from cmd without using double quotes "
My current command:
powershell.exe -noexit Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'
The error:
The string is missing the terminator: '.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
The reason I cannot use double quotes is because I'm using the post-install-command=
parameter from vboxmanage
which already uses double quotes.
CodePudding user response:
It's best to enclose everything starting with
Start-BitsTransfer
in (escaped)"..."
, i.e., to pass a single, double-quoted string to the (positionally implied)-Command
parameter ofpowershell.exe
), which preventscmd.exe
from interpreting the string's content (except, potentially,cmd.exe
-format environment-variable references such as%OS%
).Without
"..."
, as in your case, a&
outside a"..."
string is then interpreted as acmd.exe
metacharacter (statement separator), and the call breaks ('...'
strings are only recognized by PowerShell, not bycmd.exe
).While you could
^
-escape all cmd.exe metacharacters individually, enclosing the entire-Command
argument in"..."
is the simpler solution. (Also, even though it is rarely a problem in practice, whitespace normalization occurs in the absence of"..."
enclosure, i.e. runs of multiple spaces are folded into one space per run).
Since your entire
powershell.exe
command is itself embedded in a"..."
string passed as part of an option tovboxmanage
, you must escape the"
chars. that are part of thepowershell.exe
call, namely as\"
.
Therefore, in the context of your vboxmanage
call, use something like the following - note the outer "..."
and embedded inner \"...\"
:
vboxmanage ... post-install-command="powershell.exe -noexit \"Start-BitsTransfer -Source 'download link' -Destination 'C:\test.txt'\""