Bassie
Bassie

Reputation: 10400

How to Properly Escape Quotes in WMI Query

I have the following WMI Query, which polls whetehr a file has been created in C:\test :

Select * From __InstanceCreationEvent Within 1 
Where TargetInstance Isa "Cim_DirectoryContainsFile" 
and TargetInstance.GroupComponent="Win32_Directory.Name='C:\\test'"

I am able to run this in wbemtest.exe without any issue - here is the output from wbemtest :

enter image description here

However, when I try running mofcomp myfile.mof in CMD, I am getting a "unparsable query" error. In the .mof file, my query looks like this:

"Select * From __InstanceCreationEvent Within 1 "
"Where TargetInstance Isa \"Cim_DirectoryContainsFile\" "
"and TargetInstance.GroupComponent=\"Win32_Directory.Name=\"C:\\\\test\"\"";

And I really can't figure out what is wrong with my character escaping... I know that in WQL I don't need to concatenate the strings, so I don't think that is the issue. However, I have no idea whether I need to use single or double quotes, or whether a single quote inside a set of doubles needs to be escaped, or whether double-quotes are usable within a set of quotes...

The documentation on this detail is pretty much non-existent, so would really appreciate it if anyone with any experience could help me out!

Thank you

Upvotes: 1

Views: 1369

Answers (1)

Bassie
Bassie

Reputation: 10400

This filter can be written like this:

"Select * From __InstanceCreationEvent Within 1 "
"Where TargetInstance Isa \"Cim_DirectoryContainsFile\" "
"and TargetInstance.GroupComponent=\"Win32_Directory.Name=\'C:\\\\test\'\"";

and this works without issue. See this answer for more details on what happened here. It seems to be that some kind of issue with Notepad++ interferes with this formatting.

Also notice the use of single and double quotes in the filter string.

Upvotes: 1

Related Questions