using system;
using system.Collections.Generic;
using system.ComponentModel;
using system.Data;
using system.Drawing;
using system.Text;
using system.windows
.Forms;
using system.Management;
namespace Ex18_11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//指定生成 WMI 連接所需的所有設置
ConnectionOptions op = new ConnectionOptions();
op.Username = "administrator";
//遠程計算機用戶名稱
op.Password = "12345";
//遠程計算機用戶密碼
//設置操作管理范圍
ManagementScope scope = new ManagementScope("\\\\" + "121.35.174.154" + "\\root\\cimv2", op);
scope.Connect();
//將此 ManagementScope 連接到實際的 WMI 范圍。
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
//得到WMI控制
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //執行關閉遠程計算機,reboot為重新啟動
}
}
}
}