// 远程关机
public bool RemoteShutdown(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //shutdown
}
return true;
}
catch (Exception)
{
return false;
}
}
// 远程重启
public bool RemoteReboot(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("Reboot", null); //shutdown
}
return true;
}
catch (Exception)
{
return false;
}
}