对 CMI_DataFile 类的 WMI 查询产生“无效查询” 如果它具有“where 条件1 AND 条件2” (C#)

发布于 2024-07-22 17:41:39 字数 876 浏览 2 评论 0原文

好的,这就是我所做的,仅提取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2024-07-29 17:41:39

我是一个白痴。 请不要打我。

首先,只有在 WHERE 子句中传递文件名的所有组成部分时,查询才能可靠地工作。

其次,我必须在路径组件中加倍反斜杠,但我做错了。 我做到了:

pathPath.Replace("\\", "\\\\");

而我必须做的是:

pathPath = pathPath.Replace("\\", "\\\\");

没错。 我假设 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:

pathPath.Replace("\\", "\\\\");

Whereas what I had to do was:

pathPath = pathPath.Replace("\\", "\\\\");

That's right. I worked under the assumption that String.Replace() changed the string in-line. Bad C# newbie. Bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文