使用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 ...
随机推荐
- linux ssh 无密码登陆
要点:有时候ssh localhost 无法登陆本机,但各种设置都正确.重启下就好了...... 参考地址:http://bbs.csdn.net/topics/370109654 三台机器实现互相之 ...
- google 论文
从google历年所有论文的汇总来看,TOP5的分别是人工智能和机器学习.算法理论.人机交互与视觉.自然语言处理.机器感知,大家从一个侧面看出goolge research的重点了吧. Google所 ...
- PHP中的MVC
在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中.MVC足以应对大多数的情况,但还有一些情况是其不太适合的,如比较简单的个人博客,对于只有几百篇文章量级的博客,使用MVC让人觉得有些太复杂 ...
- 自己动手实现简单的Vector
看到今天,终于自己动手写了一个自己的vector,我这个版本的vector只有vector主要的一些操作,包括原版vector的所有构造函数,begin(),end(),size(),capacity ...
- Data 语义学(1)
一.Data Member 的绑定(The binding of Data Member) extern float x; class Point3d { public: Point3d( float ...
- LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)
一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...
- 如何在svn系统中使用git
如果正在使用svn,打算换到git,又暂时不想放弃已有的svn代码库,可以选择git-svn.说一说我自己从svn到git的经验吧. 开始 安装最新版本的git,从git 1.5.3以后支持git-s ...
- 汇编学习笔记(7)call和ret指令
ret和retf CPU执行ret指令时进行以下两步操作: (IP)=((ss)*16+(sp)) (sp)=(sp)+2 这相当于pop IP CPU执行retf指令时进行以下四步操作: (IP)= ...
- 高效算法——G - 贪心
G - 贪心 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Desc ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...