Search code examples
powershellwmiwql

double backslash in WMI/WQL query powershell


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.


Solution

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