使用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 ...
随机推荐
- Basic Wall Maze
poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...
- Find The Multiple(poj 1426)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- 石英晶振频率后面带的PPM是什么单位
PPM是石英晶振的基本单位之一,表示晶振的精度和相对偏差, PPM代表着百万分之一,它表明晶体的频率可能会偏离标称值多少.晶振频率是以MHZ(10的6次方)和KHZ(10的3次方)为基本单位的,标称频 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- 【转】ConcurrentModificationException异常解决办法 --不错
原文网址:http://blog.sina.com.cn/s/blog_465bcfba01000ds7.html 1月30日java.util.ConcurrentModificationExcep ...
- 【模拟】Codeforces 711A Bus to Udayland
题目链接: http://codeforces.com/problemset/problem/711/A 题目大意: N个字符串,每个字符串5位,找到第一个出现两个OO的并改成++输出YES和改后字符 ...
- 【模拟】Codeforces 710A King Moves
题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...
- Threading Module源码概述(三)
Threading中的Thread threading中一个关键的组件是threading.Thread. class Thread(_Verbose): __initialized = False ...
- angularJS constant和value
angularJS可以通过constant(name,value)和value(name,value)对于创建服务也是很重要的. 相同点是:都可以接受两个参数,name和value. 区别: 1.co ...
- UIAlertView弹出框
<Alert弹出框提示用户信息> 1.遵循代理方法<UIAlertViewDelete> 2.调用方法UIAlertView *alert = [[UIAlertV ...