简体   繁体   English

在PowerShell中转义单引号

[英]Escape single quotes in PowerShell

I'm trying to build a SharePoint 2010 listdata.svc query that resembles: 我正在尝试构建类似于以下内容的SharePoint 2010 listdata.svc查询:

http://server/FooList/_vti_bin/listdata.svc/FooList?$select=Title,Id,OwnerId,Status&$filter=(OwnerId eq 1234 and Status eq 'Ready')

Single quotes don't work: 单引号无效:

$url = $url + '&$filter=(OwnerId eq 1234 and Status eq 'Ready')'

Nor does escaping them with back-ticks: 也不用反斜杠转义:

$url = $url + '&$filter=(OwnerId eq 1234 and Status eq `'Ready`')'

If I use double quotes, $filter is replaced with a zero-length string (it's not a variable in the script): 如果我使用双引号,则将$filter替换为零长度的字符串(它不是脚本中的变量):

$url = $url + "&$filter=(OwnerId eq 1234 and Status eq 'Ready')"

What's the correct way to escape single quotes? 转义单引号的正确方法是什么?

You need to double the quotes when using single-quoted string literals: 使用单引号字符串文字时,您需要将双引号引起来:

'&$filter=(OwnerId eq 1234 and Status eq ''Ready'')'
                                         ^^     ^^

Demo: 演示:

PS > '&$filter=(OwnerId eq 1234 and Status eq ''Ready'')'
&$filter=(OwnerId eq 1234 and Status eq 'Ready')
PS >

Using backticks only works with double-quoted string literals (since they process escape sequences), but then you also need to escape all other special characters (eg the $ in variable names): 使用反引号仅适用于带双引号的字符串文字(因为它们处理转义序列),但是随后还需要转义所有其他特殊字符(例如,变量名中的$ ):

PS > "&`$filter=(OwnerId eq 1234 and Status eq `"Ready`")"
&$filter=(OwnerId eq 1234 and Status eq "Ready")
PS >

But in a single-quoted string literal, the ` is treated as a literal backtick. 但是在单引号字符串文字中, `被视为文字反引号。

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

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