【问题标题】:Escape single quotes in PowerShell在 PowerShell 中转义单引号
【发布时间】:2015-07-16 22:32:23
【问题描述】:

我正在尝试构建类似于以下内容的 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')

单引号不起作用:

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

也不用反引号来逃避它们:

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

如果我使用双引号,$filter 将替换为长度为零的字符串(它不是脚本中的变量):

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

转义单引号的正确方法是什么?

【问题讨论】:

    标签: powershell sharepoint sharepoint-2010 powershell-3.0


    【解决方案1】:

    在使用单引号字符串时需要双引号:

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

    演示:

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

    使用反引号仅适用于双引号字符串文字(因为它们处理转义序列),但您还需要转义所有其他特殊字符(例如变量名中的$):

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

    但在单引号字符串文字中,` 被视为文字反引号。

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2020-07-08
      • 1970-01-01
      • 2014-01-24
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2011-10-04
      相关资源
      最近更新 更多