我有一个以下代码,几乎所有这些代码都适用于我的大多数服务器,但对于某些特定的服务器,它只是在查询的第二部分失败。此代码查询SQL Server服务优先,第二部分查询SQL Sevrer代理服务。
我已经尝试了创建另一个Scope和Query Object的所有可能组合,但不知怎的,我试图查询的服务器没有返回第二部分,看起来像连接范围并执行第一个查询后,第二部分被阻止服务器上的东西..!对此问题的任何帮助表示赞赏..几乎99%的服务器工作正常并返回所需的结果,但只有2或3台服务器因第二部分而失败。
如果这是服务器上的WMI问题吗?有没有其他方法来实现这些状态..?像IPC或套接字..?请帮忙..!
哈希。
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()) // <---- 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());
}
答案 0 :(得分:3)
因为,正如我们在评论中讨论的那样,我们认为它可能与拥有多个活动ManagementScope
对象有关,请尝试将代码的前半部分更改为:
string agentserviceName = "SQLSERVERAGENT";
string serviceName = "MSSQLSERVER";
// Let the SelectQuery class build our WQL query text...
string className = "Win32_Service";
string condition = string.Format("Name = '{0}'", serviceName);
string[] selectedProperties = new string[] { "Name", "StartName", "State", "StartMode" };
SelectQuery query = new SelectQuery(className, condition, selectedProperties);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
searcher.Scope = new ManagementScope("\\\\" + srvName + "\\root\\cimv2");
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";
}
}
// Second query goes here...
当第一个查询完成后,它会为ManagementObjectSearcher
处理它,并确保它只保留对远程服务器ManagementScope
的唯一引用。