繁体   English   中英

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

[英]How do I escape “Quotes” in a powershell string?

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

例如:

$specialKey = "101010"
Invoke-RestMethod -Method Put `
                -Uri $keyAssignmentUri `
                -Header @{"content-type"="application/json"} `
                -Body "$specialKey"

当我在 Fiddler 中检查这个调用时,我看到正文是 101010 而不是“101010”。 如何使用引号发送正文值?

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

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

Write-Host $PrintQuotesAsString 

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

在此处输入图片说明

用单引号将其括起来以使其成为文字字符串:

$specialKey = '"101010"'

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

\`"

示例:我想(使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM