There is a code example on MSDN which uses WMI to enumerate all of the dependencies for a particular service: http://msdn.microsoft.com/en-us/library/aa393673(v=vs.85).aspx
This is great...but i've discovered that the dependencies it discovers might not all be of the same type. I was expecting all dependencies to be of type Win32_Service...but sometimes you will find that a dependency that is actually a driver (Win32_SystemDriver).
So..my question is simple - how do I adjust the MSDN code example to perform a check on each dependency and see if it's a Win32_Service or a Win32_SystemDriver so that I can handle it appropriately? Extra points if you provide the solution in jscript (the example on MSDN is vbscript, but i'm using jscript).
Try using this query:
Associators of {Win32_Service.Name="dhcp"} Where AssocClass=Win32_DependentService ResultClass=Win32_SystemDriver
to get only Win32_SystemDriver instances, or
Associators of {Win32_Service.Name="dhcp"} Where AssocClass=Win32_DependentService ResultClass=Win32_Service
to get only Win32_Service instances.
The
Win32_DependentService
association class represents dependent services using theWin32_BaseService
base class. So, if you do not define a specific ResultClass in yourASSOCIATORS OR
query (as in Uroc's answer), you'll get anyWin32_BaseService
subclasses -Win32_Service
,Win32_SystemDriver
as well asWin32_TerminalService
.To handle different object classes differently, you can check for the object's class name using the
Path_.Class
property. Here's sample JScript code that illustrates this approach: