WMI函数使用简介(二)

8、 使用WMI远程重启远程计算机

using System;
using System.Management;  
namespace WMI3
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary> 
      class Class1
      {
            static void Main(string[] args)
            {
                  Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)");
                  Console.WriteLine("mailto:Written%2002/01/02%20By%20John%20O'Donnell%20-%20csharpconsulting@hotmail.com");
                  Console.WriteLine("========================================
================================="); 
                   //连接远程计算机
            ConnectionOptions co = new ConnectionOptions();
            co.Username = "john";
            co.Password = "john";
            System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\192.168.1.2\\root\\cimv2", co);      
                  //查询远程计算机
           System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                  
           ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq);
           ManagementObjectCollection queryCollection1 = query1.Get();            
                  foreach( ManagementObject mo in queryCollection1 ) 
                  {
                        string[] ss={""};
                        mo.InvokeMethod("Reboot",ss);
                        Console.WriteLine(mo.ToString());
                  }
            }
      }
}  

9、 利用WMI创建一个新的进程

using System;
using System.Management;

// This sample demonstrates invoking a WMI method using parameter objects
public class InvokeMethod 
{    
 public static void Main() 
 {

  //Get the object on which the method will be invoked
  ManagementClass processClass = new ManagementClass("Win32_Process");

  //Get an input parameters object for this method
  ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

  //Fill in input parameter values
  inParams["CommandLine"] = "calc.exe";

  //Execute the method
  ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);

  //Display results
  //Note: The return code of the method is provided in the "returnvalue" property of the outParams object
  Console.WriteLine("Creation of calculator process returned: " + outParams["returnvalue"]);
  Console.WriteLine("Process ID: " + outParams["processId"]);
 }
}

10、 使用WMI终止一个进程

using System; 
using System.Management; 

// This example demonstrates how to stop a system service. 
class Sample_InvokeMethodOptions 
{ 
    public static int Main(string[] args) {
        ManagementObject service = 
            new ManagementObject("win32_service=\"winmgmt\"");
        InvokeMethodOptions options = new InvokeMethodOptions();
        options.Timeout = new TimeSpan(0,0,0,5); 

        ManagementBaseObject outParams = service.InvokeMethod("StopService", null, options);

        Console.WriteLine("Return Status = " + outParams["Returnvalue"]);

        return 0;
    }
}

11、 使用WMI 来获取远程机器的目录以及文件.比如如何列出一个目录下的所有文件,或者所有子目录;如何删除,舔加,更改文件

using System;
using System.Management;
// This example demonstrates reading a property of a ManagementObject.

class Sample_ManagementObject
{
        public static int Main(string[] args) {
        ManagementObject disk = new ManagementObject(
                "win32_logicaldisk.deviceid=\"c:\"");
                disk.Get();
                Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");

            return 0;
        }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值