/// <summary> /// Applies the filters to the accounts. /// </summary> public static IList <AccountInfo> ApplyFilters(AccountFilters filters, IList <AccountInfo> accounts) { if (filters == null || accounts == null) { return(accounts); } List <AccountInfo> filteredAccounts = new List <AccountInfo>(); for (int ii = 0; ii < accounts.Count; ii++) { if (accounts[ii].ApplyFilters(filters)) { filteredAccounts.Add(accounts[ii]); } } return(filteredAccounts); }
/// <summary> /// Applies the filters to the account /// </summary> public bool ApplyFilters(AccountFilters filters) { // filter on name. if (!String.IsNullOrEmpty(filters.Name)) { if (!Utils.Match(this.Name, filters.Name, false)) { return(false); } } // filter on domain. if (!String.IsNullOrEmpty(filters.Domain)) { if (String.Compare(this.Domain, filters.Domain, true) != 0) { return(false); } } // exclude non-user related accounts. if (this.SidType == AccountSidType.Domain || this.SidType > AccountSidType.BuiltIn) { return(false); } // apply account type filter. if (filters.AccountTypeMask != AccountTypeMask.None) { if ((1 << ((int)this.SidType - 1) & (int)filters.AccountTypeMask) == 0) { return(false); } } return(true); }
/// <summary> /// Applies the filters to the account /// </summary> public bool ApplyFilters(AccountFilters filters) { // filter on name. if (!String.IsNullOrEmpty(filters.Name)) { if (!Utils.Match(this.Name, filters.Name, false)) { return false; } } // filter on domain. if (!String.IsNullOrEmpty(filters.Domain)) { if (String.Compare(this.Domain, filters.Domain, true) != 0) { return false; } } // exclude non-user related accounts. if (this.SidType == AccountSidType.Domain || this.SidType > AccountSidType.BuiltIn) { return false; } // apply account type filter. if (filters.AccountTypeMask != AccountTypeMask.None) { if ((1<<((int)this.SidType-1) & (int)filters.AccountTypeMask) == 0) { return false; } } return true; }
/// <summary> /// Applies the filters to the accounts. /// </summary> public static IList<AccountInfo> ApplyFilters(AccountFilters filters, IList<AccountInfo> accounts) { if (filters == null || accounts == null) { return accounts; } List<AccountInfo> filteredAccounts = new List<AccountInfo>(); for (int ii = 0; ii < accounts.Count; ii++) { if (accounts[ii].ApplyFilters(filters)) { filteredAccounts.Add(accounts[ii]); } } return filteredAccounts; }
/// <summary> /// Queries the account table for the specified accounts. /// </summary> 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); }
/// <summary> /// Queries the account table for the specified accounts. /// </summary> 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; }