6

When using ManagementObjectSearcher - one can search for specific properties (instead of all by *).

What happens when instantiating a ManagementClass (i.e. new ManagementClass(someClass)), does it load all of the properties, or is it only some sort of pointer, and will not load the properties?

.

(I'd also be happy to know what happens when using ManagementObjectSearcher with a *, does it load anything besides the properties, or is it just like specifying all of them explicitly? Logically, it should simply mean "all", but from here it seems otherwise.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Reference source for [ManagementClass](https://referencesource.microsoft.com/#System.Management/managementclass.cs,90c78a586852b14b) – Nkosi Apr 18 '18 at 20:55
  • Reference source for [ManagementObjectSearcher](https://referencesource.microsoft.com/#System.Management/managementobjectsearcher.cs,188a1d089eab4a48) – Nkosi Apr 18 '18 at 20:57

1 Answers1

2

Nothing happens when you instantiate it. It just stores the query, scope and whatever else you may have specified. Same goes for ManagementObjectSearcher

When you call .Get(), the query is executed against the current scope. As for ManagementObjectSearcher, the actual behavior depends on whether it's a class enumeration or instance enumeration (e.g. a query)

A query is executed and directly returns results (any properties) whereas a class enumeration will bind to the underlying WMI object without actually loading anything until you call .Get() somewhere.

does it load all of the properties, or is it only some sort of pointer

It's a pointer to the CIM Object Manager. The underlying objects are only loaded when initialize is called with true (indicating that you want to bind to the underlying WMI object). This is what the queries are executed against.

Looking at the various calls to Initialize(), it seems at first glance that the objects are only bound when you're requesting properties or qualifiers. Invoking methods will not bind them.

I'd also be happy to know what happens when using ManagementObjectSearcher with a *

Nothing until you execute the query. Which, by default, performs a shallow enumeration on the object. This might return the qualifiers in addition to the properties, but I'm not entirely sure on that.

Also interesting (including the sibling articles): https://technet.microsoft.com/en-us/library/cc180561.aspx

I hope this helped at all :)

Fred Kleuver
  • 7,797
  • 2
  • 27
  • 38
  • So using `ManagementClass.InvokeMethod` will only invoke the method without wasting resources on accessing properties. Did I understand you correctly? – ispiro Apr 25 '18 at 17:56
  • As for `ManagementObjectSearcher with a *` - Yes, I meant what happens when `Get` is called. According to what I describe in [another question of mine](https://stackoverflow.com/q/49798851/939213) it looks like the only way to get the methods is to ask for `*` which seems to return more than just all the properties. Which doesn't seem logical. – ispiro Apr 25 '18 at 17:59
  • The `InvokeMethod` itself does nothing more than connecting to the ManagementScope and getting a reference to the underlying WMI object. Of course, the manner in which you retrieve the class may or may not get everything. I don't think `ManagementObjectSearcher` will help you much if you want to be as efficient as possible. Use `new ManagementClass("Name_Of_Class")` – Fred Kleuver Apr 25 '18 at 19:07