对 CMI_DataFile 类的 WMI 查询产生“无效查询” 如果它具有“where 条件1 AND 条件2” (C#)
好的,这就是我所做的,仅提取 System.Management 调用:
简单查询和方法调用在同一连接上工作。 这个查询不会。 并且该文件存在于远程计算机上。 线索?
myQuery = "Select * from CIM_DataFile Where Drive = 'C:' AND Path = '\\Users\\someguy\\Documents\\' AND FileName = 'Default' AND Extension = 'rdp'";
options = new ConnectionOptions();
options.Username = myUsername;
options.Password = myPassword;
options.Authority = "ntlmdomain:MYDOMAIN";
scope = new ManagementScope("\\\\REMOTEMACHINE\\root\\CIMV2", options);
scope.Connect();
searcher = new ManagementObjectSearcher(scope, new ObjectQuery(myQuery));
myResults = searcher.Get();
ManagementObjectSearcher.Get() 给我一个 ManagementException,说“无效查询”。 一个更简单的查询,例如“SELECT * FROM Win32_NetworkAdapter”,可以工作。
我试图将 WHERE 减少到只有一个,即“从 CIM_DataFile 中选择 *,其中扩展名 = 'rdp'”。 它有效,尽管显然它没有得到我想要的东西。 (在编辑之前,我错误地认为它即使在那时也不起作用;请参阅评论)我已经走到了尽头。
OK, here's what I'm doing distilled to only the System.Management calls:
Simple queries and Method invokes work over the same connection. This query won't. And the file exists on the remote machine. Clues?
myQuery = "Select * from CIM_DataFile Where Drive = 'C:' AND Path = '\\Users\\someguy\\Documents\\' AND FileName = 'Default' AND Extension = 'rdp'";
options = new ConnectionOptions();
options.Username = myUsername;
options.Password = myPassword;
options.Authority = "ntlmdomain:MYDOMAIN";
scope = new ManagementScope("\\\\REMOTEMACHINE\\root\\CIMV2", options);
scope.Connect();
searcher = new ManagementObjectSearcher(scope, new ObjectQuery(myQuery));
myResults = searcher.Get();
ManagementObjectSearcher.Get() gets me a ManagementException saying "Invalid query." A simpler query, like, say, "SELECT * FROM Win32_NetworkAdapter", works.
I tried to reduce the WHERE's to just one, i.e. "Select * from CIM_DataFile Where Extension = 'rdp'". It works, although obviously it doesn't get me what I want. (Before the edit I mistakenly thought it didn't work even then; see comments) I'm at the end of the rope here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是一个白痴。 请不要打我。
首先,只有在 WHERE 子句中传递文件名的所有组成部分时,查询才能可靠地工作。
其次,我必须在路径组件中加倍反斜杠,但我做错了。 我做到了:
而我必须做的是:
没错。 我假设 String.Replace() 在线更改了字符串。 糟糕的 C# 新手。 坏的。
I am an idiot. Please don't beat me up.
Firstly, the query only works reliably if you pass ALL components of the file name in WHERE clauses.
Secondly, I had to double the backslashes in the path component, and I was doing it wrong. I did:
Whereas what I had to do was:
That's right. I worked under the assumption that String.Replace() changed the string in-line. Bad C# newbie. Bad.