Escape Characters inside Cmd Strings and PowerShell Strings
Question
Friday, December 22, 2017 7:39 PM
I am writing a C program to generate an exe which is able to invoke a cmd script under the same directory and make the exe as my apps executable file, which is used for packaging the app that doesn't have an installer via Desktop App Converter.
However after launching the UWP app, at the app console, I got an error message indicating the cmd String unable to be identified inside the inner block (the PowerShell Command Block enclosed with the curly braces '{}'):
%%command%% : The term '%%command%%' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ %%command%%
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (%%command%%:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Press any key to continue . . .
So why is the variable %command% inaccessible inside the PowerShell session? However, the really confusing thing was the reason why it showed an error of the term '%%command%%' instead of '%command%'.
In order to indicate a constant whose data type is 'const char *', the escape character '%%' was used within the double quotes of the character array.
Here is the C source code:
#include <stdlib.h>
int main( void )
{
system( "@echo off & set command=Get-AppxPackage^ -AllUsers^ ^|^ Where-Object^ {^ $_.PackageFullName^ -like^ ^\"*Unknown.LanguagePackConverter*^\"^ }^ ^|^ ForEach-Object^ {^ PowerShell^ -Command^ ^\"$($_.InstallLocation)\\run.cmd^\"^ } & PowerShell -Command %%command%% & pause" );
return 0;
}
However, if I declared variable $command within the PowerShell session, then I encountered issues with escaping double quotes '""' inside the PowerShell String. I wrote the source code as:
#include <stdlib.h>
int main( void )
{
system( "@echo off & PowerShell -Command \"& { $command = \'Get-AppxPackage -AllUsers | Where-Object { $_.PackageFullName -like `\"*Unknown.LanguagePackConverter*`\" } | ForEach-Object { PowerShell -Command `\"$($_.InstallLocation)\\run.cmd`\" }\'; PowerShell -Command $command }\" & pause" );
return 0;
}
Here is the console output:
At line:1 char:68
+ ... pxPackage -AllUsers | Where-Object { $_.PackageFullName -like `*Unkno ...
+ ~
You must provide a value expression following the '-like' operator.
At line:1 char:69
+ ... { $_.PackageFullName -like `*Unknown.LanguagePackConverter*` } | ForE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '`*Unknown.LanguagePackConverter*` ' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
Press any key to continue . . .
All replies (2)
Saturday, December 23, 2017 3:39 AM
Could you please identify which one or more characters inside the Cmd Strings or PowerShell Strings above are ambiguous that have to get escaped?
Wednesday, December 27, 2017 8:51 AM
Hi Sam Iris,
thanks for posting here.
>>Could you please identify which one or more characters inside the Cmd Strings or PowerShell Strings above are ambiguous that have to get escaped?
Here is a list of special characters to escape.
Here are some documents about special characters in PowerShell, you could refer to.
https://blogs.technet.microsoft.com/csps/2010/07/14/special-characters-part-1/
Besides, this forum is about c++ code issues. For more information about command line, you could post on these forums below.
https://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverpowershell
https://social.technet.microsoft.com/forums/en-us/home
Your understanding and cooperation will be grateful.
Best Regards,
Baron Bi
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.