2015-05-06 43 views
0

我試圖建立一個SharePoint 2010 listdata.svc查詢類似於逃逸單引號:在PowerShell中

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被替換爲零長度h字符串(它不是腳本中的變量):

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

什麼是逃避單引號的正確方法?

回答

3

您需要將報價翻倍使用單引號的字符串常量時:

'&$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 > 

但在單引號字符串文字中,`被視爲文字反引號。