3

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.

Roxx
  • 3,738
  • 20
  • 92
  • 155

2 Answers2

6

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.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • yes, you are correct above query is just an example. Can you please give me some example regarding this. – Roxx Jul 09 '15 at 17:43
  • @404 That depends on what the source of the query is and how you plan on inserting it. – Matt Jul 09 '15 at 17:45
  • a\100104 is also coming from WMI query. When i output this value. It is giving me a\100104. Now i have to pass it to another WQL. – Roxx Jul 09 '15 at 17:48
  • @404 If that is the case I don't see why what me and Mathias suggested would not work. `$resultofFirstQuery.replace("\","\\")`. If you use double quotes for the here string then you can just add the variable directly in the query – Matt Jul 09 '15 at 18:29
  • It works perfectly but i am getting one error. Exception setting "CurrentState": "Cannot convert value "Pending" to type "System.UInt32". Error: "Input string was not in a correct format."" – Roxx Jul 10 '15 at 06:44
3

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 '\\','\\'
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • But i am putting the value in quote like this 'a\100104' or "a\100104". Is there anyway to achieve. – Roxx Jul 09 '15 at 17:42
  • 2
    PowerShell does not care about the slash in this case... WQL cares regardless – Matt Jul 09 '15 at 17:43
  • @404 Single-quotes only prevent the PowerShell parser from interpreting the value one way or another, it's the WMI provider that "misunderstands" you – Mathias R. Jessen Jul 09 '15 at 17:43
  • Ok. Thanks for clarification. Currently user id is like this a\100104. That i will have to pass in WMI by where clause. I can't remove backslash from this. I need to find a way so, WMi can understand it. – Roxx Jul 09 '15 at 17:46