在 C# 中使用 WMI 获取 Windows 服务器状态
Get Windows Server status using WMI in C#
我需要在 Windows 服务器上自动启动的服务。
谁能告诉我一种使用 WMI 获取 Windows 服务器状态(联机或脱机)的方法。
提前致谢
请尝试下面的代码,
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"]);
}
我需要在 Windows 服务器上自动启动的服务。 谁能告诉我一种使用 WMI 获取 Windows 服务器状态(联机或脱机)的方法。
提前致谢
请尝试下面的代码,
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"]);
}