I'm having some issues with WMI queries and I'm not sure why they're occurring or how to fix them. I'm writing a simple program to do simple WMI queries on remote machines on our network (we're building something to check information on our PCs for inventory so that we don't have to go to all of them manually).
The query works but only if the query statement is "hard coded" in the program; if I try to take the info from a text box (so that we can type the queries in during run time) it gives a "Not Found" exception.
Here is the code I'm using:
This code works:
try
{
//string targetcomp = this.ComputerNameTextBox.Text; // Collect computer name
//string wmiquery = this.QueryTextBox.Text.ToString(); // Collect WMI query
//string qval = this.QueryTextBox.Text; // Collect Query value requested
// Connect to computer
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + targetcomp + "\\root\\cimv2", options);
scope.Connect();
// Query variables
ObjectQuery query;
ManagementObjectSearcher searcher;
ManagementObjectCollection queryCollection;
// Query - OS Name/Version
query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
searcher = new ManagementObjectSearcher(scope, query);
searcher.Options.ReturnImmediately = true; // If a computer doesn't respond, stop trying
queryCollection = searcher.Get();
this.ResultsTextBox.Text += "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
foreach (ManagementObject m in queryCollection)
{
//if (m[qval] == null)
if (m["TotalPhysicalMemory"] == null)
{
this.ResultsTextBox.Text += "TotalPhysicalMemory" + ": [Value Not Found]";
}
else
{
this.ResultsTextBox.Text += "TotalPhysicalMemory" + ": " + m["TotalPhysicalMemory"].ToString() + "\n";
}
}
this.ResultsTextBox.Text += "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n\n";
}
catch (Exception MyError)
{
//MessageBox.Show("An error has occurred: " + MyError.Message);
MessageBox.Show("An error has occurred: " + MyError.ToString());
}
This code gives the exception:
try
{
string targetcomp = this.ComputerNameTextBox.Text; // Collect computer name
string wmiquery = this.QueryTextBox.Text.ToString(); // Collect WMI query
string qval = this.QueryTextBox.Text; // Collect Query value requested
// Connect to computer
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + targetcomp + "\\root\\cimv2", options);
scope.Connect();
// Query variables
ObjectQuery query;
ManagementObjectSearcher searcher;
ManagementObjectCollection queryCollection;
// Query - OS Name/Version
query = new ObjectQuery(wmiquery);
//query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
searcher = new ManagementObjectSearcher(scope, query);
searcher.Options.ReturnImmediately = true; // If a computer doesn't respond, stop trying
queryCollection = searcher.Get();
this.ResultsTextBox.Text += "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
foreach (ManagementObject m in queryCollection)
{
if (m[qval] == null)
{
this.ResultsTextBox.Text += qval + ": [Value Not Found]";
}
else
{
this.ResultsTextBox.Text += qval + ": " + m[qval].ToString() + "\n";
}
}
this.ResultsTextBox.Text += "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n\n";
}
catch (Exception MyError)
{
//MessageBox.Show("An error has occurred: " + MyError.Message);
MessageBox.Show("An error has occurred: " + MyError.ToString());
}
I had seen something on a website saying that a random space at the beginning or end of the query string would cause the "Not Found" exception... however, after printing the query string collected from the text box, with delimiters on each side, clearly there are no leading/trailing spaces or newlines or anything...
Anyone have any idea why this isn't working? Or how I can fix it?
We want to be able to type WMI queries into the text box and then run them, rather than having the queries hard coded at compile time.
Thanks!
-Justin

New Topic/Question
Reply




MultiQuote





|