需要监控服务器的一些系统信息,可以通过wmi接口进行数据读取:
/// <summary>
/// 获取数据
/// </summary>
public static void get()
{
ManagementPath pathSearch = new ManagementPath("\\\\122.224.1.12\\Root\\CIMV2");
ManagementScope scope = new ManagementScope(pathSearch);
scope.Options.Username = "user";
scope.Options.Password = "pwd";
SelectQuery select = new SelectQuery("select * from win32_logicaldisk");
//SelectQuery select = new SelectQuery("SELECT * FROM Win32_NTLogEvent where TimeWritten>='2011-1-3' and Logfile='SYSTEM'");
ManagementObjectSearcher search = new ManagementObjectSearcher(select);
search.Scope = scope;
int i = 0;
foreach (ManagementObject obj in search.Get())
{
if (obj["DriveType"].ToString() != "3")
continue;
Console.WriteLine(obj["Name"].ToString());
Console.WriteLine(obj["VolumeName"].ToString());
Console.WriteLine(obj["Size"].ToString());
Console.WriteLine(obj["FreeSpace"].ToString());
////if (obj["EventType"].ToString() != "1")
//// continue;
//Console.WriteLine(obj["Message"].ToString());
////Console.WriteLine(obj["VolumeName"].ToString());
////Console.WriteLine(obj["Size"].ToString());
////Console.WriteLine(obj["FreeSpace"].ToString());
}
}
/// <summary>
/// 判断是32位系统还是64位系统
/// </summary>
/// <returns></returns>
public static string Detect3264()
{
ManagementPath pathSearch = new ManagementPath( \\\\122.224.1.12\\Root\\CIMV2 );
ManagementScope scope = new ManagementScope(pathSearch);
scope.Options.Username = "user";
scope.Options.Password = "Pwd";
SelectQuery select = new SelectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher search = new ManagementObjectSearcher(select);
search.Scope = scope;
string addressWidth = null;
foreach (ManagementObject oReturn in search.Get())
{
addressWidth = oReturn["AddressWidth"].ToString();
}
return addressWidth;
}
通过wmi接口远程访问有安全隐患,解决方法:
1.IP安全策略,需要开启wmi服务和135端口。
2.防火墙过滤,和Ip策略类似
遇到的问题
返回:“没有发现指定的此对象导出者”。
在本地机器的目录:C:\WINDOWS\system32\drivers\etc,在此目录下的hosts文件中增加一行:<目标机器ip> <目标机器计算机名>