添加删除WMI名称空间

查了N多的资料,也找不到一个添加WMI的空间的例子,找到的都是添加WMI类的例子。
后来微软的人,得到了添加自己创建的wmi名称空间的代码。我们可以在我们自己定义的名称空间下,添加一些wmi类,记录一些信息,供远程wmi读取信息。结合wmi远程调用程序的方法,可以用来客户服务器通讯。

[windows2003 测试通过]
添加wmi名称空间:
       仿照windows\system32\wbem下的mof文件,创建一个自己的mof文件放到这个目录里,
        使用命令"mofcomp.exe yourfile.mof" 来创建自己的名称空间

使用代码来添加名称空间:
         
None.gif  ConnectionOptions options  =   new  ConnectionOptions();
None.gif            options.Username 
=  user;  // could be in domain\user format
None.gif
            options.Password  =  password;
None.gif 
None.gif            ManagementScope scope 
=   new  ManagementScope( string .Format( @" \\{0}\root " ,compName), options);
None.gif 
None.gif            ManagementClass Class 
=   new  ManagementClass(scope,  new  ManagementPath( " __namespace " ),  new  ObjectGetOptions());
None.gif 
None.gif            ManagementObject instance 
=  Class.CreateInstance();
None.gif 
None.gif            instance[
" Name " =   " mytest " ;
None.gif            instance.Put();
None.gif
注意是为远程Client添加wmi名称空间,如若要给当前系统添加wmi名称空间,则不需要也不能使用ConnetionOption(网络连接)。
添加名称空间后的效果
WMI%5Bnamespace%5D.JPG

删除wmi名称空间:
None.gif             ConnectionOptions options  =   new  ConnectionOptions();
None.gif            options.Username 
=  user;  // could be in domain\user format
None.gif
            options.Password  =  password;
None.gif 
None.gif            ManagementScope scope 
=   new  ManagementScope( string .Format( @" \\{0}\root " ,compName), options);
None.gif 
None.gif            ManagementClass Class 
=   new  ManagementClass(scope,  new  ManagementPath( " __namespace " ),  new  ObjectGetOptions());
None.gif 
None.gif            ManagementObject instance 
=  Class.CreateInstance();
None.gif 
None.gif            instance[
" Name " =   " mytest " ;
None.gif            instance.Delete();
None.gif

远程执行命令的wmi
None.gif     ManagementClass win32_process  =   new  ManagementClass(scope,  new  ManagementPath( " Win32_process " ),  null );
None.gif
None.gif            ManagementBaseObject inParamaters 
=  win32_process.Methods[ " Create " ].InParameters;
None.gif            inParamaters[
" CommandLine " =   " c:\app.exe " ;
None.gif            inParamaters[
" CurrentDirectory " =   " c:\ " ;
None.gif
None.gif            ManagementBaseObject outParamater 
=  win32_process.InvokeMethod( " Create " , inParamaters,  null );            
None.gif
None.gif            
//  now wait for the setup program to finish
None.gif
             string  query  =   string .Format( " select * from __instanceDeletionEvent within 2 where targetInstance isa 'win32_process' and targetInstance.ProcessID = {0} "
None.gif                outParamater[
" ProcessId " ]);
None.gif            WqlEventQuery q 
=   new  WqlEventQuery(query);
None.gif
None.gif            ManagementEventWatcher w 
=   new  ManagementEventWatcher(scope, q,  new  EventWatcherOptions( null new  TimeSpan( 0 0 5 0 ),  1 ));
None.gif
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {   
InBlock.gif                w.WaitForNextEvent();            
ExpandedBlockEnd.gif            }

None.gif            
catch (Exception e)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
// Todo: possibly take out this print because it is handled.
InBlock.gif
                Console.WriteLine(string.Format("Handled: {0} during test automation installation.", e.Message));
ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/skyfei/archive/2005/04/08/134078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值