1. wmi连接前提
利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码。如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以。
2. 相关类的用法--- ConnectionOptions和ManagementScope
ConnectionOptions用于设置连接选项,比如设置所连接机器的域,用户名,密码等。ManagementScope用于连接的实际操作。
: using System;
: using System.Collections.Generic;
: using System.Text;
: using System.Management;
:
: namespace TJVictor.WMI
: {
: public class WMIBaseClass
: {
: #region Property
: private ConnectionOptions connection;
:
: private ManagementScope scope;
: public ManagementScope Scope
: {
: get { return scope; }
: set { scope = value; }
: }
:
: private string domain;
: public string Domain
: {
: get { return domain; }
: set { domain = value; }
: }
:
: private string ip;
: public string Ip
: {
: get { return ip; }
: set { ip = value; }
: }
:
: private string user;
: public string User
: {
: get { return user; }
: set { user = value; }
: }
:
: private string password;
: public string Password
: {
: get { return password; }
: set { password = value; }
: }
:
: private string nameSpace;
: public string NameSpace
: {
: get { return nameSpace; }
: set { nameSpace = value; }
: }
: #endregion
:
: #region Construction
: public WMIBaseClass()
: {
: this.domain = string.Empty;
: this.ip = string.Empty;
: this.user = string.Empty;
: this.password = string.Empty;
: this.nameSpace = "root//cimv2";
: }
:
: public WMIBaseClass(string ip, string user, string password)
: {
: this.domain = string.Empty;
: this.ip = ip;
: this.user = user;
: this.password = password;
: this.nameSpace = "root//cimv2";
: }
:
: public WMIBaseClass(string domain, string ip, string user, string password)
: {
: this.domain = domain;
: this.ip = ip;
: this.user = user;
: this.password = password;
: this.nameSpace = "root//cimv2";
: }
:
: public WMIBaseClass(string domain, string ip, string user, string password, string nameSpace)
: {
: this.domain = domain;
: this.ip = ip;
: this.user = user;
: this.password = password;
: this.nameSpace = nameSpace;
: }
: #endregion
:
: #region protected function
: protected virtual void Connection()
: {
: this.scope = Cache.CacheClass.Get(this.ip) as ManagementScope;
: if (this.scope == null)
: {
: connection = new ConnectionOptions();
: if (!domain.Equals(string.Empty))
: connection.Authority = "ntlmdomain:" + this.domain;
: if (!user.Equals(string.Empty))
: connection.Username = this.user;
: if (!password.Equals(string.Empty))
: connection.Password = this.password;
: string temp = string.Empty;
: if (ip.Equals(string.Empty))
: temp = "////" + "." + "//" + this.nameSpace;////" + this.ip + "//" + this.nameSpace;
: else
: temp = "
: scope = new ManagementScope(temp, connection);
: if (!scope.IsConnected)
: scope.Connect();
: Cache.CacheClass.Insert(this.ip, scope);
: }
: else
: {
: if (!scope.IsConnected)
: scope.Connect();
: }
: }
:
: protected virtual void DisConnection(string key)
: {
: Cache.CacheClass.Remove(key);
: }
:
: protected virtual ManagementObjectCollection GetSelectQueryCollection(string wqlSelect, string condition)
: {
: SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
: ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
: return mos.Get();
: }
:
: protected virtual ManagementObjectSearcher GetObjectSearcher(string wqlSelect, string condition)
: {
: SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
: return new ManagementObjectSearcher(scope, query);
: }
: #endregion
: }
: } . 代码说明 由于连接远端机器是所有wmi操作的第一步,所以我们把连接wmi作为一个基类,以后所有对wmi操作的类都继承这个类。 其中Connection()函数就是建立远端连接。其实很简单,如果只要把域、用户名、密码、IP、wmi命名空间等属性设置完成,就可以利用wmi提供的scope.Connect();来尝试连接远端机器。 Wmi中没有释放连接的函数。也就是说,当这个类被GC回收后,远端连接也就自动被释放了,否则与远端机器一直都处于连接状态。