This should seriously be a trivial escaping issue, but I can't query WMI for a group like so:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent="Win32_Group.Domain='TESTWIN7DEVEL-P',Name='Alaenna's Smile'"
This returns a WMI error as shown here using wbemtest
:
I tried escaping with a backslash, as described in another stackoverflow post:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent="Win32_Group.Domain='TESTWIN7DEVEL-P',Name='Alaenna\'s Smile'"
This also doesn't work. From another article, I read the following:
\ should be interpreted as a single backslash, and \" should be interpreted as a single quote
This query succeeds, but returns zero results. Because double quotes aren't permitted in Windows group names, I can't create a test group to see if the query is pulling a non-existent group called 'Alaenna"s Smile" or something else:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent="Win32_Group.Domain='TESTWIN7DEVEL-P',Name='Alaenna\"s Smile'"
I know for a fact that this query works as intended with groups without quotes. The following is an example query without quotes and the intended results:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent="Win32_Group.Domain='TESTWIN7DEVEL-P',Name='Administrators'"
How do I escape a single quote group name using the WMI query above?
This is being used in code to query for the members of a group (e.g. "Alaenna's Smile") in C#. I'd be happy with another solution that can accomplish getting the members of a all system groups or similar. I just figured this was the best, most direct route.
After playing around with it for a while I've found that switching the quoting scheme should allow for you to properly escape what you're looking for.
Instead of:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent="Win32_Group.Domain='TESTWIN7DEVEL-P',Name='Alaenna\'s Smile'"
Try:
SELECT * FROM WIN32_GroupUser WHERE GroupComponent='Win32_Group.Domain="TESTWIN7DEVEL-P",Name="Alaenna\'s Smile"'
Notice that double and single quotes are essentially switched, except for within the string Alaenna's Smile
.