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;
}
}