9 Replies - 1278 Views - Last Post: 30 March 2010 - 05:52 AM Rate Topic: -----

#1 beju0506  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-February 08

WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 09:55 AM

Hey everyone,

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

Is This A Good Question/Topic? 0
  • +

Replies To: WMI queries hardcoded working, from text box produces "Not Found&#

#2 LetMeFinclOut  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 14-May 09

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 11:31 AM

This may just be a copy paste error, but in the working code, you're sulling the computer name from the targetcomp variable which you have not defined. Other than that, it looks like it shouls work.

I'd start out by adding some debugging code to make sure both methods produce the same object.

For example:
ObjectQuery query1, query2;
query1 = new ObjectQuery("SELECT * FROM Win32_ComputerSystem"); //hardcoded query
query2 = new ObjectQuery(wmiquery);                             //query retrieved from textbox
if (!query1.Equals(query2)
{
	throw new ApplicationException("query1.Equals(query2) = false");
}



Just try to dial in on exactly where the error is occurring. (It's not always where you think it is.)

This post has been edited by Tsunami14: 29 March 2010 - 11:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 11:52 AM

Why are you putting delimiters in your query? To ensure there are no spaces at the beginning or end of the query use this (also there is no need to use ToString() when getting a value from a TextBox since it's considered a string by default)

string wmiquery = this.QueryTextBox.Text.Trim();



One last thing, are you typing this exact query SELECT * FROM Win32_ComputerSystem?
Was This Post Helpful? 1
  • +
  • -

#4 beju0506  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-February 08

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:04 PM

PsychoCoder,

Understood about the ToString() thing... I didn't originally have that, but I am trying all avenues to get this thing to work. I figured this must have something to do with conversion, so I thought maybe there was an issue there :) I just left it in when I copied to the post.

I tried the Trim(), didn't make any difference. Same problem.

Yup, I'm 100% sure I'm copying and pasting that EXACT string. In fact, I also copied it from your post and tried it, no luck.

Any ideas?

Thanks!

-Justin


View PostPsychoCoder, on 29 March 2010 - 10:52 AM, said:

Why are you putting delimiters in your query? To ensure there are no spaces at the beginning or end of the query use this (also there is no need to use ToString() when getting a value from a TextBox since it's considered a string by default)

string wmiquery = this.QueryTextBox.Text.Trim();



One last thing, are you typing this exact query SELECT * FROM Win32_ComputerSystem?


Tsunami,

I tried this and it says they don't match... so that's clearly the problem. The question for me is, why? I have no idea... it's really bizarre. It must be something I'm missing, but I don't get it.

Any idea?

Thanks!

-Justin


View PostTsunami14, on 29 March 2010 - 10:31 AM, said:

This may just be a copy paste error, but in the working code, you're sulling the computer name from the targetcomp variable which you have not defined. Other than that, it looks like it shouls work.

I'd start out by adding some debugging code to make sure both methods produce the same object.

For example:
ObjectQuery query1, query2;
query1 = new ObjectQuery("SELECT * FROM Win32_ComputerSystem"); //hardcoded query
query2 = new ObjectQuery(wmiquery);                             //query retrieved from textbox
if (!query1.Equals(query2)
{
	throw new ApplicationException("query1.Equals(query2) = false");
}



Just try to dial in on exactly where the error is occurring. (It's not always where you think it is.)

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:08 PM

Start with trying to hand write the query, maybe copy & paste is putting some character in the text that we cannot see. Also, what is the value you're putting into this.QueryTextBox.Text?

I would start by putting a breakpoint on this line

string targetcomp = this.ComputerNameTextBox.Text;


And use F11 to step line by line to see exactly what line the error is happening on
Was This Post Helpful? 2
  • +
  • -

#6 beju0506  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-February 08

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:13 PM

Psychocoder,

Soo...... clearly I haven't had enough sleep or I'm a moron (possibly both). I was assigning the entire query string to the "qval" variable as well as to the "wmiquery" variable. I noticed it when I was stepping through with F11. I changed it to the proper text box and now it's working fine...

Sorry for wasting everyone's time :(

I really appreciate your help and patience lol...

Thanks again!

-Justin


View PostPsychoCoder, on 29 March 2010 - 11:08 AM, said:

Start with trying to hand write the query, maybe copy & paste is putting some character in the text that we cannot see. Also, what is the value you're putting into this.QueryTextBox.Text?

I would start by putting a breakpoint on this line

string targetcomp = this.ComputerNameTextBox.Text;


And use F11 to step line by line to see exactly what line the error is happening on

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:14 PM

Not wasting my (or anyone else's) at all, we're just for that reason. Glad you got it working :)
Was This Post Helpful? 0
  • +
  • -

#8 beju0506  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-February 08

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:21 PM

PsychoCoder,

Thanks, I appreciate it :) Without you guys, I'd still be trying to figure out why my strings weren't equal ;) hehehe

-Justin

View PostPsychoCoder, on 29 March 2010 - 11:14 AM, said:

Not wasting my (or anyone else's) at all, we're just for that reason. Glad you got it working :)

Was This Post Helpful? 0
  • +
  • -

#9 LetMeFinclOut  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 115
  • Joined: 14-May 09

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 29 March 2010 - 12:54 PM

Always glad to help you help yourself. So, I'd consider this a success.

Good job! :^:

This post has been edited by Tsunami14: 29 March 2010 - 12:55 PM

Was This Post Helpful? 1
  • +
  • -

#10 beju0506  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-February 08

Re: WMI queries hardcoded working, from text box produces "Not Found&#

Posted 30 March 2010 - 05:52 AM

Thanks, Tsunami! :)

I've always found this forum to be a great resource and hopefully someday I can give back as much as I've gotten from it.

Thanks again!

-Justin


View PostTsunami14, on 29 March 2010 - 11:54 AM, said:

Always glad to help you help yourself. So, I'd consider this a success.

Good job! :^:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1