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-BitsTransferin (escaped)"...", i.e., to pass a single, double-quoted string to the (positionally implied)-Commandparameter ofpowershell.exe), which preventscmd.exefrom 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.exemetacharacter (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-Commandargument 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.execommand is itself embedded in a"..."string passed as part of an option tovboxmanage, you must escape the"chars. that are part of thepowershell.execall, 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'\""
