使用WMI来连接远端计算机
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回收后,远端连接也就自动被释放了,否则与远端机器一直都处于连接状态。
使用WMI来连接远端计算机的更多相关文章
- 如何通过SecureCRTPortable.exe 软件远程连接某个计算机(或者虚拟机)中的某个数据库
1)双击SecureCRTPortable.exe - 快捷方式,打开软件; 2)"文件"--->"快速连接"-->弹出对话框: 2.1)输入主机名 ...
- Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)
问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...
- 远程桌面连接无法验证您希望连接的计算机的身份-mac连接远程桌面
在使用Mac(苹果笔记本)连接远程桌面的时候提示:”远程桌面连接无法验证您希望连接的计算机的身份”,具体异常如截图:解决方法如下:1. 登录云服务器.2. 打开运行,然后输入命令gpedit.msc, ...
- 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库
孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...
- 孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库
孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第二 ...
- mac下远程win8.1时提示"桌面连接无法验证您希望连接的计算机的身份"的解决办法
在os x下使用远程桌面到win8.1,总出现“远程桌面连接无法验证您希望连接的计算机的身份”的提示. 解决方法:1.网上各种解释,包括防火墙是否打开,是否去掉“仅允许运行使用网络级别身份验证的远程计 ...
- PL/SQL配置和连接远端数据库
1. 安装与配置 (1) 安装 因为是免安装的绿色版,所以解压到目录就可以了,保证目录中没有空格.下划线和中文字符. 还有一点,PL/SQL需要和Oracle的版本一致,都是32位或者都是64位,否则 ...
- WMI 连接远程计算机并进行局域网进程扫描
On Error Resume Next Dim myArray(231) myArray(0)="smss.exe"myArray(1)="csrss.exe" ...
- python 模块 wmi 远程连接 windows 获取配置信息
测试工具应用: https://ask.csdn.net/questions/247013 wmi连接不上报错问题集 https://blog.csdn.net/xcntime/article/det ...
随机推荐
- 【技术贴】解决使用maven jetty启动后无法加载修改过后的静态资源
如何使用jetty自动热部署修改后的所有文件,比如js,jpg,class等,哇咔咔 太爽啦比tomcat舒服多了. jetty模式是不能修改js文件的,比如你现在调试前端js,发现在myeclips ...
- K Smallest Sums
uva11997:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- MYSQL的硬盘IO过高引起的CPU过高判断
其实,为客户提供相关日志,不就是RACKSPACE主要作的事? 俺们以后也可以效仿的.不要去解决,而是协助客户定位. http://blog.const.net.cn/a/17275.htm 上文的思 ...
- zabbix 部分item采集间隔调整
- 使用next-key locks 用于搜索和索引扫描,可以防止幻读
Next-Key Locks A next-key lock is a combination of a record lock on the index record and a gap lock ...
- Oracle 多版本控制
SESSION 1: SQL> create table t 2 as 3 select * from all_users; Table created. SQL> variable x ...
- linux配置端口转发
一.使用rinted进行端口转发 将10.50.13.13 80请求转到10.50.13.11 80上 1.安装rinetd $ tar zxf rinetd.tar.gz $ cd rinetd $ ...
- 2015第37周一struts2 jstl 标签
1.在jstl中使用struts2 <c:forEach var="ee" items="${requestScope.serviceList}" &g ...
- 图论 BZOJ 3669 [Noi2014]魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- poj 1149 最大流
题目链接:http://poj.org/problem?id=1149 #include <cstdio> #include <cmath> #include <algo ...