Hi, I'm hoping someone can make sense of this...
I'm trying to use WMI to get file information from a specific directory on a remote machine using CIM_DataFile. I can get all of the files on the on the remote machine using:
System.Management.ObjectQuery oq1 = new System.Management.ObjectQuery("SELECT * FROM CIM_DataFile");
But when I try to narrow it down to the specific directory, I get an Invalid Query error (pasted at the end of this post). I'm using this query for the specific directory:
System.Management.ObjectQuery oq1 = new System.Management.ObjectQuery("SELECT * FROM CIM_DataFile WHERE Name = 'c:\\pats\\bin'");
I've also tried:
System.Management.ObjectQuery oq1 = new System.Management.ObjectQuery(@"SELECT * FROM CIM_DataFile WHERE Name = 'c:\pats\bin'");
And I've tried both of the above, ending the WHERE Name = path with a \. No difference.
The method I've written:
public void TryWMIConnect(string machdomain, string machname, string username, string passwd)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = machdomain + "\\" + username;
options.Password = passwd;
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Packet;
options.Timeout = new TimeSpan(0, 0, 0, 5);
string RemoteMachine = "\\\\" + machname + "\\root\\cimv2";
ManagementScope scope = new ManagementScope(RemoteMachine, options);
bool wmiSuccess = false;
try
{
scope.Connect();
wmiSuccess = true;
}
catch (System.Runtime.InteropServices.COMException e)
{
Trace.WriteLine(e.Message);
Trace.WriteLine(machname, " WMI Failure");
}
if (wmiSuccess)
{
System.Management.ObjectQuery oq1 = new System.Management.ObjectQuery("SELECT * FROM CIM_DataFile WHERE Name = 'c:\\pats\\bin'");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq1);
ManagementObjectCollection queryCollection1 = query1.Get();
try
{
Trace.WriteLine(queryCollection1.Count); //This is where the error occurrs - or, at least, it reports after this line execution
}
catch (Exception e)
{
Trace.WriteLine(e);
}
}
}
Error thrown:
System.Management.ManagementException: Invalid query
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at System.Management.ManagementObjectCollection.get_Count()
at PatsUpdateDev.Config.TryWMIConnect(String machdomain, String machname, String username, String passwd) in C:\Users\maxmel\Documents\Visual Studio 2008\Projects\PatsUpdateDev\PatsUpdateDev\Config.cs:line 156
System.Management.ManagementException: Invalid query
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at PatsUpdateDev.Config.TryWMIConnect(String machdomain, String machname, String username, String passwd) in C:\Users\maxmel\Documents\Visual Studio 2008\Projects\PatsUpdateDev\PatsUpdateDev\Config.cs:line 165
Thanks in advance for any advice you may be able to provide...