我如何逃避"行情"在powershell字符串?

时间:2017-06-30 07:11:49

标签: rest powershell

我需要使用Powershell向web api发送一个简单的字符串,并且我必须确保HTML正文中的值具有" Quotes"围绕它,因为内容类型必须是application / json。

例如:

var moveTask = Task.Run(() => File.Move("source", "Target"));
// Do something else
await moveTask;

当我在Fiddler中检查这个电话时,我发现身体是101010而不是" 101010"。如何使用引号发送正文值?

3 个答案:

答案 0 :(得分:6)

用单引号括起来使它成为文字字符串:

$specialKey = '"101010"'

答案 1 :(得分:4)

为了拥有"引号",你需要在每个"之前提供转义字符(`)。您要打印或发送的内容。

$PrintQuotesAsString = "`"How do I escape `"Quotes`" in a powershell string?`"" 

Write-Host $PrintQuotesAsString 

"How do I escape "Quotes" in a powershell string?"

enter image description here

答案 2 :(得分:4)

用一系列反斜杠、反引号和双引号替换双引号 (") 对我有用:

\`"

示例:我想(使用 env 命令)将名为 SCOPES 的环境变量设置为以下值,然后启动 node app.js

{
   "email":"Grant permissions to read your email address",
   "address":"Grant permissions to read your address information",
   "phone":"Grant permissions to read your mobile phone number"
}

所以我使用了:

env SCOPES="{ \`"email\`": \`"Grant permissions to read your email address\`",   \`"address\`": \`"Grant permissions to read your address information\`",   \`"phone\`": \`"Grant permissions to read your mobile phone number\`" }" node app.js

参考文献:http://www.rlmueller.net/PowerShellEscape.htm