首页 > 解决方案 > 如何使用值中的与号将字符串从cmd传递到powershell?

问题描述

在 Windows CMD 中,我想运行一个 powershell 脚本并传递字符串参数,这些参数在参数中包含引号和 & 符号。

U:\Desktop>PowerShell .\post "http://domain/api/app?pLanguage=en-US\"&\"pCompanyID=816" "{\"Message\":\"test message - please ignore\"}"
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

'\"pCompanyID=816"' is not recognized as an internal or external command,
operable program or batch file.

我该如何解决这个问题?

标签: windowspowershellcmd

解决方案


来自本网站上的另一个问题 -如何在命令行 powershell 参数中转义 & 符号、分号和花括号?

将命令行字符串用嵌套的双引号和单引号括起来——例如“'[somestring]'”。

在您的具体情况下:

C:> powershell .\post "'http://domain/api/app?pLanguage=en-US&pCompanyID=816'" "'{\"Message\":\"test message - please ignore\"}'"

推荐阅读