如何在powershell字符串中转义"引号"?

wil*_*lem 5 rest powershell

我需要一个简单的字符串发送到使用PowerShell网络API,我必须确保在HTML体值有"行情",围绕它,因为它的内容类型必须是application/JSON.

例如:

$specialKey = "101010"
Invoke-RestMethod -Method Put `
                -Uri $keyAssignmentUri `
                -Header @{"content-type"="application/json"} `
                -Body "$specialKey"
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*agg 7

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

$specialKey = '"101010"'
Run Code Online (Sandbox Code Playgroud)


小智 7

为了获得“报价”,您需要在每个要打印或发送的“”之前提供转义字符(`)。

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

Write-Host $PrintQuotesAsString 

"How do I escape "Quotes" in a powershell string?"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Sta*_*opo 6

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

\`"
Run Code Online (Sandbox Code Playgroud)

示例:我想(使用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"
}
Run Code Online (Sandbox Code Playgroud)

所以我用:

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
Run Code Online (Sandbox Code Playgroud)

参考资料:http : //www.rlmueller.net/PowerShellEscape.htm