最近在学习WMI.NET,主要的任务需求是写一个IIS WMI Provider来实现对IIS自动的监控,管理。所以在网上搜寻了很多关于WMI.NET的信息,
今天我们主要来展示一下使用WMI.NET来枚举计算机用户和组。
涉及到计算机用户和用户组的WMI Class主要有以下四类:
1. Win32_Account
2. Win32_UserAccount
3. Win32_Group
4. Win32_GroupUser
为了获取所有用户,你需要执行此查询语句:
select * from Win32_Account where Domain='YOURDOMAIN'
为了获取所有的用户组,你需要执行此查询语句:
Select * from Win32_GroupUser where Domain='YOURDOMAIN'
获取所有计算机用户代码如下:
代码
/// <summary>
/// 获取所有用户
/// </summary>
public static void GetUsers()
{
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='LMY-PC'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
Console.WriteLine("User Accounts");
Console.WriteLine("");
foreach (ManagementObject mObject in mSearcher.Get())
{
Console.WriteLine(mObject["Name"]);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
/// 获取所有用户
/// </summary>
public static void GetUsers()
{
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='LMY-PC'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
Console.WriteLine("User Accounts");
Console.WriteLine("");
foreach (ManagementObject mObject in mSearcher.Get())
{
Console.WriteLine(mObject["Name"]);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
获取计算机的所有用户组的代码如下:
代码
/// <summary>
/// 获取所有用户组
/// </summary>
public static void GetGroups()
{
SelectQuery sQuery = new SelectQuery("Win32_Group", "Domain='LMY-PC'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
Console.WriteLine("User Groups");
Console.WriteLine("");
foreach (ManagementObject mObject in mSearcher.Get())
{
Console.WriteLine(mObject["Name"]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
/// 获取所有用户组
/// </summary>
public static void GetGroups()
{
SelectQuery sQuery = new SelectQuery("Win32_Group", "Domain='LMY-PC'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
Console.WriteLine("User Groups");
Console.WriteLine("");
foreach (ManagementObject mObject in mSearcher.Get())
{
Console.WriteLine(mObject["Name"]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
获取计算机特定用户组的所有用户的代码如下:
代码
public static void GetUsers(String DomainName, string GroupName)
{
#region Bulid WMI query using SelectQuery
StringBuilder sBuilder = new StringBuilder("GroupComponent=");
sBuilder.Append('"');
sBuilder.Append("Win32_Group.Domain=");
sBuilder.Append("'");
sBuilder.Append(DomainName);
sBuilder.Append("'");
sBuilder.Append(",Name=");
sBuilder.Append("'");
sBuilder.Append(GroupName);
sBuilder.Append("'");
sBuilder.Append('"');
SelectQuery sQuery = new SelectQuery("Win32_GroupUser", sBuilder.ToString());
#endregion
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
foreach (ManagementObject mObject in mSearcher.Get())
{
ManagementPath path = new ManagementPath(mObject["PartComponent"].ToString());
if (path.ClassName =="Win32_UserAccount")
{
string[] names = path.RelativePath.Split(',');
Console.WriteLine(names[1].Substring(names[1].IndexOf("=") + 1).Replace('"', ' ').Trim());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
{
#region Bulid WMI query using SelectQuery
StringBuilder sBuilder = new StringBuilder("GroupComponent=");
sBuilder.Append('"');
sBuilder.Append("Win32_Group.Domain=");
sBuilder.Append("'");
sBuilder.Append(DomainName);
sBuilder.Append("'");
sBuilder.Append(",Name=");
sBuilder.Append("'");
sBuilder.Append(GroupName);
sBuilder.Append("'");
sBuilder.Append('"');
SelectQuery sQuery = new SelectQuery("Win32_GroupUser", sBuilder.ToString());
#endregion
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
foreach (ManagementObject mObject in mSearcher.Get())
{
ManagementPath path = new ManagementPath(mObject["PartComponent"].ToString());
if (path.ClassName =="Win32_UserAccount")
{
string[] names = path.RelativePath.Split(',');
Console.WriteLine(names[1].Substring(names[1].IndexOf("=") + 1).Replace('"', ' ').Trim());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}