1. wmi连接前提

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

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

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

  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. : scope = new ManagementScope(temp, connection);
  113. : if (!scope.IsConnected)
  114. : scope.Connect();
  115. : Cache.CacheClass.Insert(this.ip, scope);
  116. : }
  117. : else
  118. : {
  119. : if (!scope.IsConnected)
  120. : scope.Connect();
  121. : }
  122. : }
  123. :
  124. : protected virtual void DisConnection(string key)
  125. : {
  126. : Cache.CacheClass.Remove(key);
  127. : }
  128. :
  129. : protected virtual ManagementObjectCollection GetSelectQueryCollection(string wqlSelect, string condition)
  130. : {
  131. : SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
  132. : ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
  133. : return mos.Get();
  134. : }
  135. :
  136. : protected virtual ManagementObjectSearcher GetObjectSearcher(string wqlSelect, string condition)
  137. : {
  138. : SelectQuery query = new SelectQuery(string.Format(wqlSelect, condition));
  139. : return new ManagementObjectSearcher(scope, query);
  140. : }
  141. : #endregion
  142. : }
  143. : }
  144.  
  145. . 代码说明
  146.  
  147. 由于连接远端机器是所有wmi操作的第一步,所以我们把连接wmi作为一个基类,以后所有对wmi操作的类都继承这个类。
  148.  
  149. 其中Connection()函数就是建立远端连接。其实很简单,如果只要把域、用户名、密码、IP、wmi命名空间等属性设置完成,就可以利用wmi提供的scope.Connect();来尝试连接远端机器。
  150.  
  151. Wmi中没有释放连接的函数。也就是说,当这个类被GC回收后,远端连接也就自动被释放了,否则与远端机器一直都处于连接状态。

使用WMI来连接远端计算机的更多相关文章

  1. 如何通过SecureCRTPortable.exe 软件远程连接某个计算机(或者虚拟机)中的某个数据库

    1)双击SecureCRTPortable.exe - 快捷方式,打开软件; 2)"文件"--->"快速连接"-->弹出对话框: 2.1)输入主机名 ...

  2. Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)

    问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...

  3. 远程桌面连接无法验证您希望连接的计算机的身份-mac连接远程桌面

    在使用Mac(苹果笔记本)连接远程桌面的时候提示:”远程桌面连接无法验证您希望连接的计算机的身份”,具体异常如截图:解决方法如下:1. 登录云服务器.2. 打开运行,然后输入命令gpedit.msc, ...

  4. 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库

    孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...

  5. 孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库

    孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第二 ...

  6. mac下远程win8.1时提示"桌面连接无法验证您希望连接的计算机的身份"的解决办法

    在os x下使用远程桌面到win8.1,总出现“远程桌面连接无法验证您希望连接的计算机的身份”的提示. 解决方法:1.网上各种解释,包括防火墙是否打开,是否去掉“仅允许运行使用网络级别身份验证的远程计 ...

  7. PL/SQL配置和连接远端数据库

    1. 安装与配置 (1) 安装 因为是免安装的绿色版,所以解压到目录就可以了,保证目录中没有空格.下划线和中文字符. 还有一点,PL/SQL需要和Oracle的版本一致,都是32位或者都是64位,否则 ...

  8. WMI 连接远程计算机并进行局域网进程扫描

    On Error Resume Next Dim myArray(231) myArray(0)="smss.exe"myArray(1)="csrss.exe" ...

  9. python 模块 wmi 远程连接 windows 获取配置信息

    测试工具应用: https://ask.csdn.net/questions/247013 wmi连接不上报错问题集 https://blog.csdn.net/xcntime/article/det ...

随机推荐

  1. Basic Wall Maze

    poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...

  2. Find The Multiple(poj 1426)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  3. 石英晶振频率后面带的PPM是什么单位

    PPM是石英晶振的基本单位之一,表示晶振的精度和相对偏差, PPM代表着百万分之一,它表明晶体的频率可能会偏离标称值多少.晶振频率是以MHZ(10的6次方)和KHZ(10的3次方)为基本单位的,标称频 ...

  4. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  5. 【转】ConcurrentModificationException异常解决办法 --不错

    原文网址:http://blog.sina.com.cn/s/blog_465bcfba01000ds7.html 1月30日java.util.ConcurrentModificationExcep ...

  6. 【模拟】Codeforces 711A Bus to Udayland

    题目链接: http://codeforces.com/problemset/problem/711/A 题目大意: N个字符串,每个字符串5位,找到第一个出现两个OO的并改成++输出YES和改后字符 ...

  7. 【模拟】Codeforces 710A King Moves

    题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...

  8. Threading Module源码概述(三)

    Threading中的Thread threading中一个关键的组件是threading.Thread. class Thread(_Verbose): __initialized = False ...

  9. angularJS constant和value

    angularJS可以通过constant(name,value)和value(name,value)对于创建服务也是很重要的. 相同点是:都可以接受两个参数,name和value. 区别: 1.co ...

  10. UIAlertView弹出框

    <Alert弹出框提示用户信息>    1.遵循代理方法<UIAlertViewDelete>    2.调用方法UIAlertView *alert = [[UIAlertV ...