I’m trying to return all enabled features from a remote win2008 server. And that wasn’t any problem at all really – as long as I knew what exactly to query.
The problems I’m having however is when my query doesn’t find a result, it takes forever to validate if the feature is installed or not – sometimes up to 2 minutes. (Not good enough when querying over 600 nodes).
The following code is the fastest way I’ve found of doing it, however as I said: it takes forever to return false:
public bool serverFeatureEnabled(string machineName, Win32_ServerFeature_ID id)
{
ManagementClass serviceClass = new ManagementClass("Win32_ServerFeature");
string strScope = string.Format(@"\\{0}\root\cimv2", machineName);
ConnectionOptions conOpt = new ConnectionOptions();
serviceClass.Scope = new ManagementScope(strScope, conOpt);
foreach (ManagementObject obj in serviceClass.GetInstances())
{
if ((UInt32)obj["ID"] == (uint)id)
{
return true;
}
}
return false;
}
Does anyone have a better idea of doing this, I don’t mind if it not using WMI queries at all.
All I want to do is to speed it up a bit really.
I hope I made some sense!
Any help is appreciated.
Edit:
I have tried to “directly select required feature from server features with ManagementObjectSearcher class” as proposed by Sergrey V.
It did speed up the return of the first false, however it takes around 14 sec to finish, that all adds upp to 140 minutes of all the servers queried in the cluster.
Edit 2:
I tried to run tests against Win32_ServerFeature using WBEMTEST (Windows Management Instrumentation Tester), the connection to a remote computer seems to be the problem – running the test against one of the remote computers takes about 12 seconds to connect and around 2 seconds to return the data.
So the solution proposed by Sergrey V seems to be the fastest solution for WMI queries so far.
Don’t run the action in series. I don’t know if WMI has a bottleneck internally but the most obvious optimisation is to run the entire thing in Parallel.