使用WMI查找服务的依赖关系,然后区分依赖服务与依赖驱动程序 [英] Use WMI to find dependencies of a service and then differentiate dependent Services from dependent Drivers

查看:270
本文介绍了使用WMI查找服务的依赖关系,然后区分依赖服务与依赖驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN上有一个代码示例,它使用WMI枚举特定服务的所有依赖项: http://msdn.microsoft.com/en-us/library/aa393673(v = vs85).aspx

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

这是非常好的...但是我发现它发现的依赖关系可能并不是一样的。我期望所有的依赖关系都是Win32_Service的类型...但是有时你会发现一个实际上是驱动程序(Win32_SystemDriver)的依赖项。

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).

很简单 - 如何调整MSDN代码示例来对每个依赖项执行检查,看看它是Win32_Service还是Win32_SystemDriver,以便我可以适当地处理它?如果您在jscript中提供解决方案,额外的点数(MSDN上的示例是vbscript,但是我使用的是jscript)。

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).

推荐答案

Win32_DependentService 关联类表示使用 Win32_BaseService 基类的依赖服务。因此,如果您没有在<$ c中定义一个特定的 ResultClass $ c> ASSOCIATORS OR 查询(如 Win32_BaseService 子类 - Win32_Service Win32_SystemDriver 以及 Win32_TerminalService

The Win32_DependentService association class represents dependent services using the Win32_BaseService base class. So, if you do not define a specific ResultClass in your ASSOCIATORS OR query (as in Uroc's answer), you'll get any Win32_BaseService subclasses - Win32_Service, Win32_SystemDriver as well as Win32_TerminalService.

为了处理不同的对象类,您可以使用 Path_.Class 属性。以下是JScript代码示例:

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:

var strComputer = ".";
var strServiceName = "RpcSs";

var oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" + strComputer + "/root/cimv2");

var colItems = oWMI.ExecQuery("ASSOCIATORS OF {Win32_Service.Name='" + strServiceName + "'} WHERE AssocClass=Win32_DependentService Role=Antecedent");
var enumItems = new Enumerator(colItems);

var oItem;
for ( ; !enumItems.atEnd(); enumItems.moveNext()) {
  oItem = enumItems.item();

  switch (oItem.Path_.Class) {
    case "Win32_Service":
      ...
      break;
    case "Win32_TerminalService":
      ...
      break;
    case "Win32_SystemDriver":
      ...
      break;
    default:
      // another class
      ...
      break;
  }
}

这篇关于使用WMI查找服务的依赖关系,然后区分依赖服务与依赖驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆