如何正确逃避WMI查询中的报价

时间:2016-04-26 11:18:50

标签: c++ windows-7 automation wmi wql

我有以下WMI查询,它会在C:\test中创建文件时进行轮询:

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

我可以在wbemtest.exe中运行它而没有任何问题 - 这是wbemtest的输出:

enter image description here

但是,当我尝试在CMD中运行mofcomp myfile.mof时,我收到“不可解析的查询”错误。在.mof文件中,我的查询如下所示:

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

我真的无法弄清楚我的角色逃脱有什么问题...... 我知道在WQL中我不需要连接字符串,所以我不认为这是问题。但是,我不知道是否需要使用单引号或双引号,或者是否需要转义一组双精度内的单引号,或者是否在一组引号中使用双引号...

关于这个细节的文档几乎不存在,所以如果有经验的人可以帮助我,我将非常感激!

谢谢

1 个答案:

答案 0 :(得分:1)

此过滤器可以这样写:

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

这没有问题。有关此处发生的更多详情,请参阅this answer。似乎Notepad++的某些问题会干扰此格式化。

另请注意在过滤字符串中使用单引号和双引号。

相关问题