Cmd字符串和PowerShell字符串中的转义字符 [英] Escape Characters inside Cmd Strings and PowerShell Strings

查看:252
本文介绍了Cmd字符串和PowerShell字符串中的转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C程序来生成一个exe,它能够在同一目录下调用cmd脚本,并将exe作为我的应用程序可执行文件,用于打包没有的应用程序通过Desktop App Converter安装。

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.

然而,在启动UWP应用程序后,在应用程序控制台上,我收到一条错误消息,指示无法在内部块中识别cmd String(PowerShell)用大括号"{}"括起来的命令块:

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 . . .




那么为什么PowerShell会话中的变量%命令%不可访问?然而,真正令人困惑的是,它显示了术语'%%命令%%'而不是'%command%'的错误。

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%'.

为了表示一个常数,数据类型是'const char *',转义字符'%%'在字符数组的双引号中使用。

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.

这是C源代码:

#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;
}




但是,如果我在PowerShell会话中声明了变量$ command,然后我遇到了在PowerShell字符串中转义双引号"""的问题。我将源代码编写为:

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;
}

这是控制台输出:

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 . . .

推荐答案

在PowerShell中,转义字符是反引号," " ;.但是如果字符串用双引号引用,你也可以通过加倍来转义任何嵌入的双引号字符,如"""中所示。我会用'"替换命令中的任何双引号
In PowerShell the escape character is the backtick, "`". But if the string is quoted with double quotes, you can also escape any embedded double quote characters, ", by doubling them, as in "". I would replace any double quotes in the command with `".


这篇关于Cmd字符串和PowerShell字符串中的转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆