我發現這個代碼在一個古老的線程來關閉本地計算機:WMI重新啓動遠程計算機
using System.Management;
void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}
是否有可能使用類似的WMI方法重新啓動標誌「2」的遠程機器,爲此,我只有機器名稱,而不是IP地址。
編輯:我目前有:
SearchResultCollection allMachinesCollected = machineSearch.FindAll();
Methods myMethods = new Methods();
string pcName;
ArrayList allComputers = new ArrayList();
foreach (SearchResult oneMachine in allMachinesCollected)
{
//pcName = oneMachine.Properties.PropertyNames.ToString();
pcName = oneMachine.Properties["name"][0].ToString();
allComputers.Add(pcName);
MessageBox.Show(pcName + "has been sent the restart command.");
Process.Start("shutdown.exe", "-r -f -t 0 -m \\" + pcName);
}
但是,這並不工作,我寧願WMI前進。
作爲參考,「標誌」的價值2是什麼把這個關機請求變成重新啓動。完整的標誌列表在這裏:https://msdn.microsoft.com/en-us/library/aa394058(v=vs.85).aspx – Ceilingfish 2015-10-26 16:01:05