Double backslash in WMI / WQL powershell query

I am facing a weird problem 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

      

The above query is not working correctly, but when I add another backslash in FCM.CollectionID

like this ( a\\100104

) it starts working.

Can someone please tell me why it works like this? I cannot manually put backslashes on all values ​​as they will later be generated from another WMI query.

+3


source to share


2 answers


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 backtrack character (`).

In your generated queries, you can simply artificially add a forward slash with simple replacement if you like.



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

      

I'm sure I $SCCMQuery

was just a test case, but the request has to be put in code somewhere.

+3


source


Even though it \

is a literal character in PowerShell, it is still an escape character in WQL.

From the specification :

WQL uses terms and concepts as specified in [DMTF-DSP0004] , except as noted below, and requires familiarity with the CIM. The server MUST treat the backslash as an escape character in the WQL query , except for the LIKE clause. The server MUST treat a string literal in WQL data as case insensitive, contrary to what [DMTF-DSP0004] indicates.



Usage can use an operator -replace

to strip backslashes (remember that the first argument -replace

is a regex pattern, which also treats backslashes as an escape character):

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

      

+3


source







All Articles