public static IList<AccountInfo> Query(AccountFilters filters)
{
if (filters == null)
{
filters = new AccountFilters();
}
List<AccountInfo> accounts = new List<AccountInfo>();
StringBuilder builder = new StringBuilder();
if (!String.IsNullOrEmpty(filters.Domain))
{
// check for non-existent domain.
if (String.Compare(filters.Domain, System.Net.Dns.GetHostName(), true) != 0)
{
if (String.IsNullOrEmpty(LookupDomainSid(filters.Domain)))
{
return accounts;
}
}
builder.AppendFormat("Domain='{0}'", filters.Domain);
}
SelectQuery query = new SelectQuery("Win32_Account", builder.ToString());
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
try
{
foreach (ManagementObject target in searcher.Get())
{
// filter on SID type.
byte? sidType = target["SIDType"] as byte?;
if (sidType == null || sidType.Value == 3 || sidType.Value > 5)
{
continue;
}
// create account info object.
AccountInfo account = new AccountInfo();
account.Name = target["Name"] as string;
account.SidType = (AccountSidType)sidType.Value;
account.Sid = target["SID"] as string;
account.Domain = target["Domain"] as string;
account.Description = target["Description"] as string;
account.Status = target["Status"] as string;
if (account.ApplyFilters(filters))
{
accounts.Add(account);
}
}
}
finally
{
searcher.Dispose();
}
return accounts;
}