我需要在Windows Server上自动启动的服务。 有人可以告诉我一种使用WMI获取Windows服务器状态(在线或离线)的方法。
提前谢谢
答案 0 :(得分:0)
请尝试以下代码,
string FullComputerName = "<Name of Remote Computer>";
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\\\" + FullComputerName + "\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalService");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject queryObj in queryCollection)
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_TerminalService instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Started: {0}", queryObj["Started"]);
Console.WriteLine("State: {0}", queryObj["State"]);
Console.WriteLine("Status: {0}", queryObj["Status"]);
}