title: WMI throwing Exception "Invalid Query" on second attempt to query the same server
body: <p>I have a following code, almost all the time this code works for most of my server, but for some specific servers, it just fails on second part of query. this code query the SQL Server Service First and in second part it queries the SQL Sevrer Agent Service.</p>
  
  <p>I have tried all possible combination of creating another Scope and Query Object, but somehow the server I am trying to query does not return seccond part, looks like after the scope is connected and first query is executed, second part is blocked by something on server..! Any help on this one is appreciated.. almost 99% of servers works fine and returns desire results, but just 2 or 3 servers fails for second part..</p>
  
  <p>If this is WMI issue on the server it self..? is there any other way to achieve these status..? like IPC or Sockets..? please help..!</p>
  
  <p>Hash.</p>
  
  <pre><code>try
  {
      agentserviceName = "SQLSERVERAGENT";
      serviceName = "MSSQLSERVER";
      query = new System.Management.SelectQuery(string.Format("select name, startname, State, StartMode from Win32_Service where name = '{0}'", serviceName));
      ManagementScope scope = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
      //ManagementScope scope = new ManagementScope("\\\\ST0176V\\root\\cimv2");
      scope.Connect();
      System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(scope, query);
  
      // MessageBox.Show((String)dgv_ChangeSvcAccount.Rows[i].Cells[1].Value.ToString());
      foreach (ManagementObject service in searcher.Get())
      {
          dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = service["startname"];
          dgv_ChangeSvcAccount.Rows[i].Cells[4].Tag = serviceName;
          dgv_ChangeSvcAccount.Rows[i].Cells[5].Value = "Currently : " + service["State"] + " - Set As : " + service["StartMode"];
      }
  
      if (searcher.Get().Count == 0)
      {
          dgv_ChangeSvcAccount.Rows[i].Cells[4].Value = "NO SQL Service Found";
      }
  
      searcher.Dispose();
  
      ManagementScope scope2 = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
  
      // ObjectQuery query2 = new ObjectQuery("SELECT * FROM Win32_Service WHERE NAME LIKE '" + serviceName.ToString().ToUpper() + "'");
  
      System.Management.SelectQuery query2 = new System.Management.SelectQuery(string.Format("select name, startname, State, StartMode from Win32_Service where name like '{0}'", agentserviceName));
      System.Management.ManagementObjectSearcher searcher1 = new System.Management.ManagementObjectSearcher(scope2, query2);
      foreach (ManagementObject service in searcher1.Get()) // &lt;---- this line throws exception for invalid query, and it is always 2 servers which does that, rest of servers returns proper results. the servers which throws this Invlid Query exceptions are Windows 2000 Server with SP4.
      {
          dgv_ChangeSvcAccount.Rows[i].Cells[6].Value = service["startname"];
          dgv_ChangeSvcAccount.Rows[i].Cells[6].Tag = agentserviceName;
          dgv_ChangeSvcAccount.Rows[i].Cells[7].Value = "Currently : " + service["State"] + " - Set As : " + service["StartMode"];
      }
      searcher1.Dispose();        
  }
  catch (System.Exception ex)
  {
      MessageBox.Show(ex.Message.ToString());
  }
  </code></pre>