Look at this cmd.exe command: powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}""-f 'ec','ho') hello"
powershell reports error:
C:\Users\Administrator>powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}""-f 'ec','ho') hello"
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Seems that a " terminator is missing?
But wrote a C program that outputs argv line by line, to see what the arguments really expanded to:
#include <iostream>
using namespace std;
int wmain(int argc, wchar_t* argv[])
{
for (int i = 0; i < argc; i++) {
wcout << i << L":" << argv[i] << endl;
}
}
C:\Users\Administrator\source\repos\ArgvDemo\x64\Debug>.\ArgvDemo.exe powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}""-f 'ec','ho') hello"
0:.\ArgvDemo.exe
1:powershell.exe
2:-NonInteractive
3:-NoProfile
4:-Command
5:&("{0}{1}"-f 'ec','ho') hello
The last argument parsed just fine!
And the powershell command &("{0}{1}"-f 'ec','ho') hello runs fine:
PS C:\Users\Administrator> &("{0}{1}"-f 'ec','ho') hello
hello
And actually if I add an extra " to the command in question:
powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}""-f 'ec','ho') hello"
Add " here ^
powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}"""-f 'ec','ho') hello"
It runs fine:
C:\Users\Administrator\source\repos\ArgvDemo\x64\Debug>powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}"""-f 'ec','ho') hello"
hello
But if I put it in my C program:
C:\Users\Administrator\source\repos\ArgvDemo\x64\Debug>.\ArgvDemo.exe powershell.exe -NonInteractive -NoProfile -Command "&(""{0}{1}"""-f 'ec','ho') hello"
0:.\ArgvDemo.exe
1:powershell.exe
2:-NonInteractive
3:-NoProfile
4:-Command
5:&("{0}{1}"-f
6:'ec','ho')
7:hello
What happened?
What I understand is that double quote "" inside a pair of quote " will be parsed to a single quote: "arg: ""quote""!" = arg: "quote"!. But is it really the case?