wmiwmi-queryget-wmiobject

How can I escape single quotes in a WMI query?


I have the following WMI query:

SELECT PartComponent FROM Win32_GroupUser WHERE GroupComponent = "Win32_Group.Domain='DOMAIN_NAME',Name='Opérateurs d'assistance de contrôle d'accès'"

The single quotes in the group name mess with the query syntax and lead to the following error:

System.Management.ManagementException: Invalid query

I tried:

Nothing worked, the error remains the same.

I am using .NET (System.Management.ManagementObjectSearcher), but it can also be tested using PowerShell:

Get-WmiObject -Query "SELECT PartComponent FROM Win32_GroupUser WHERE GroupComponent = ""Win32_Group.Domain='DOMAIN_NAME',Name='Opérateurs d'assistance de contrôle d'accès'"""

How do I properly escape the single quotes in the group name?


Solution

  • Changing all regular double quotes to single quotes and vice versa AND escaping the single quotes in the group name using \'

    Result in PowerShell:

    Get-WmiObject -Query "SELECT PartComponent FROM Win32_GroupUser WHERE GroupComponent = 'Win32_Group.Domain=""DOMAIN_NAME"",Name=""Opérateurs d\'assistance de contrôle d\'accès""'"