double backslash in WMI/WQL query powershell

半世苍凉 提交于 2019-12-10 18:06:19

问题


I am facing weird issue with my WQL query.

$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.

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.


回答1:


If you look at about_wql you will see that

WQL uses the backslash () as its escape character. This is different from Windows PowerShell, which uses the backtick character (`).

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.




回答2:


Even though \ is just a literal character in PowerShell, it's still an escape character in 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. The server MUST treat a backslash as an escape character in a WQL query, except within a LIKE clause. The server MUST treat literal strings in WQL data as case-insensitive, contrary to what [DMTF-DSP0004] specifies.

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):

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


来源:https://stackoverflow.com/questions/31324415/double-backslash-in-wmi-wql-query-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!