如何正确转义 WMI 查询中的引号

How to Properly Escape Quotes in WMI Query

我有以下 WMI 查询,它会轮询文件是否已在 C:\test 中创建:

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

我能够 运行 在 wbemtest.exe 中做到这一点没有任何问题 - 这是 wbemtest 的输出:

但是,当我在 CMD 中尝试 运行ning mofcomp myfile.mof 时,出现 "unparsable query" 错误。在 .mof 文件中,我的查询如下所示:

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

而且我真的想不通我的角色转义出了什么问题... 我知道在 WQL 中我不需要连接字符串,所以我认为这不是问题所在。但是,我不知道我是否需要使用单引号或双引号,或者是否需要转义一组双引号中的单引号,或者是否可以在一组引号中使用双引号...

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

谢谢

这个过滤器可以这样写:

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

这没有问题。有关这里发生的事情的更多详细信息,请参阅 。似乎 Notepad++ 的某种问题干扰了这种格式。

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