使用WMI来连接远端计算机

1. wmi连接前提

利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码。如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以。

2. 相关类的用法--- ConnectionOptions和ManagementScope

ConnectionOptions用于设置连接选项,比如设置所连接机器的域,用户名,密码等。ManagementScope用于连接的实际操作。

代码如下:

+展开
-C#
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using System.Management;
   5:   
   6:  namespace TJVictor.WMI
   7:  {
   8:      public class WMIBaseClass
   9:      {
  10:          #region Property
  11:          private ConnectionOptions connection;
  12:   
  13:          private ManagementScope scope;
  14:          public ManagementScope Scope
  15:          {
  16:              get { return scope; }
  17:              set { scope = value; }
  18:          }
  19:   
  20:          private string domain;
  21:          public string Domain
  22:          {
  23:              get { return domain; }
  24:              set { domain = value; }
  25:          }
  26:   
  27:          private string ip;
  28:          public string Ip
  29:          {
  30:              get { return ip; }
  31:              set { ip = value; }
  32:          }
  33:   
  34:          private string user;
  35:          public string User
  36:          {
  37:              get { return user; }
  38:              set { user = value; }
  39:          }
  40:   
  41:          private string password;
  42:          public string Password
  43:          {
  44:              get { return password; }
  45:              set { password = value; }
  46:          }
  47:   
  48:          private string nameSpace;
  49:          public string NameSpace
  50:          {
  51:              get { return nameSpace; }
  52:              set { nameSpace = value; }
  53:          }
  54:          #endregion
  55:   
  56:          #region Construction
  57:          public WMIBaseClass()
  58:          {
  59:              this.domain = string.Empty;
  60:              this.ip = string.Empty;
  61:              this.user = string.Empty;
  62:              this.password = string.Empty;
  63:              this.nameSpace = "root\\cimv2";
  64:          }
  65:   
  66:          public WMIBaseClass(string ip, string user, string password)
  67:          {
  68:              this.domain = string.Empty;
  69:              this.ip = ip;
  70:              this.user = user;
  71:              this.password = password;
  72:              this.nameSpace = "root\\cimv2";
  73:          }
  74:   
  75:          public WMIBaseClass(string domain, string ip, string user, string password)
  76:          {
  77:              this.domain = domain;
  78:              this.ip = ip;
  79:              this.user = user;
  80:              this.password = password;
  81:              this.nameSpace = "root\\cimv2";
  82:          }
  83:   
  84:          public WMIBaseClass(string domain, string ip, string user, string password, string nameSpace)
  85:          {
  86:              this.domain = domain;
  87:              this.ip = ip;
  88:              this.user = user;
  89:              this.password = password;
  90:              this.nameSpace = nameSpace;
  91:          }
  92:          #endregion
  93:   
  94:          #region protected function
  95:          protected virtual void Connection()
  96:          {
  97:              this.scope = Cache.CacheClass.Get(this.ip) as ManagementScope;
  98:              if (this.scope == null)
  99:              {
 100:                  connection = new ConnectionOptions();
 101:                  if (!domain.Equals(string.Empty))
 102:                      connection.Authority = "ntlmdomain:" + this.domain;
 103:                  if (!user.Equals(string.Empty))
 104:                      connection.Username = this.user;
 105:                  if (!password.Equals(string.Empty))
 106:                      connection.Password = this.password;
 107:                  string temp = string.Empty;
 108:                  if (ip.Equals(string.Empty))
 109:                      temp = "\\\\" + "." + "\\" + this.nameSpace;\\\\" + this.ip + "\\" + this.nameSpace;
 110:                  else
 111:                      temp = "
 112:   
 113:                  scope = new ManagementScope(temp, connection);
 114:   
 115:                  if (!scope.IsConnected)
 116:                      scope.Connect();
 117:   
 118:                  Cache.CacheClass.Insert(this.ip, scope);
 119:              }
 120:              else
 121:              {
 122:                  if (!scope.IsConnected)
 123:                      scope.Connect();
 124:              }
 125:          }
 126:   
 127:          protected virtual void DisConnection(string key)
 128:          {
 129:              Cache.CacheClass.Remove(key);
 130:          }
 131:   
 132:          protected virtual ManagementObjectCollection GetSelectQueryCollection(string wqlSelect, string condition)
 133:          {
 134:              SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
 135:              ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
 136:              return mos.Get();
 137:          }
 138:   
 139:          protected virtual ManagementObjectSearcher GetObjectSearcher(string wqlSelect, string condition)
 140:          {
 141:              SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
 142:              return new ManagementObjectSearcher(scope, query);
 143:          }
 144:          #endregion
 145:      }
 146:  }


3. 代码说明

由于连接远端机器是所有wmi操作的第一步,所以我们把连接wmi作为一个基类,以后所有对wmi操作的类都继承这个类。

其中Connection()函数就是建立远端连接。其实很简单,如果只要把域、用户名、密码、IP、wmi命名空间等属性设置完成,就可以利用wmi提供的scope.Connect();来尝试连接远端机器。

Wmi中没有释放连接的函数。也就是说,当这个类被GC回收后,远端连接也就自动被释放了,否则与远端机器一直都处于连接状态。

如需转载,请注明本文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor

加支付宝好友偷能量挖...


评论(0)网络
阅读(81)喜欢(0)Asp.Net/C#/WCF