简体   繁体   English

WMI / WQL查询powershell中的双反斜杠

[英]double backslash in WMI/WQL query powershell

I am facing weird issue with my WQL query. 我的WQL查询面临着奇怪的问题。

$SCCMQuery = @'
Select UAR.User, UAR.Application, UAR.CurrentState from sms_fullcollectionmembership as FCM
INNER JOIN SMS_UserApplicationRequest as UAR ON UAR.User=FCM.SMSID
where FCM.CollectionID="a\100104"
'@
$Results = Get-WmiObject -Namespace "root\SMS\Site_Name" -Query $SCCMQuery

Above query is not working properly but when i add another backslash in FCM.CollectionID like this( a\\\\100104 ) then it start working. 上面的查询不能正常工作但是当我在FCM.CollectionID添加另一个反斜杠时( a\\\\100104 )然后它开始工作。

Can somebody please advise me why it is working like this? 有人可以告诉我为什么它这样工作? I can't manually put the backslash in all the values as they will later be generated from other WMI query. 我无法手动将反斜杠放在所有值中,因为它们稍后将从其他WMI查询生成。

If you look at about_wql you will see that 如果你看看about_wql,你会看到

WQL uses the backslash () as its escape character. WQL使用反斜杠()作为转义字符。 This is different from Windows PowerShell, which uses the backtick character (`). 这与使用反引号字符(`)的Windows PowerShell不同。

In your generated queries you can just artificially add the slash with a simple replace if you wanted. 在生成的查询中,如果需要,您可以通过简单替换人为地添加斜杠。

$SCCMQuery.replace("\","\\")

I am sure that $SCCMQuery was just a testing example but the query has to be placed in the code somewhere. 我确信$SCCMQuery只是一个测试示例,但查询必须放在某处的代码中。

Even though \\ is just a literal character in PowerShell, it's still an escape character in WQL. 即使\\只是PowerShell中的文字字符,它仍然是WQL中的转义字符

From the specification : 规格

WQL uses terminologies and concepts, as specified in [DMTF-DSP0004] , except as noted below, and requires familiarity with the CIM model. WQL使用[DMTF-DSP0004]中规定的术语和概念,除非如下所述,并且需要熟悉CIM模型。 The server MUST treat a backslash as an escape character in a WQL query , except within a LIKE clause. 除了在LIKE子句中之外,服务器必须将反斜杠视为WQL查询中的转义字符 The server MUST treat literal strings in WQL data as case-insensitive, contrary to what [DMTF-DSP0004] specifies. 服务器必须将WQL数据中的文字字符串视为不区分大小写,与[DMTF-DSP0004]指定的相反。

Use can use the -replace operator to escape the backslash (be aware that the first argument to -replace is a regex pattern which also treats backslash as an escape character): 使用可以使用-replace运算符来转义反斜杠(请注意, -replace的第一个参数是一个正则表达式模式,它也将反斜杠视为转义字符):

$escapedWmiQueryValue = $WmiQueryValue -replace '\\','\\'

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

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