I've got some code that uses WMI to scour a windows domain for computers matching certain criteria.
I get a COMException If I'm unable to communicate with the computer I'm trying to query. When testing on a large domain, this can result in thousands of exceptions being thrown, which is very expensive performance-wise.
Is there a way for me to check for a valid connection, BEFORE querying, so that I can prevent these errors from happening?
Simplified Example:
foreach(var computer in domain) {
var scope = new ManagementScope(computerPath, options), query);
try {
using (var searcher = new ManagementObjectSearcher(scope)) {
if (searcher.Get().Count == 0) {
// do stuff
}
}
} catch(ComException e) {
// Log and continue
}
}