我在尋找解決方案的時間相當長,但我發現所有解決方案都非常慢。 我想讓本地窗口組中的所有用戶。該組當然也可以含有AD組。因此,結果應包含屬於該組本身的成員的所有用戶和所包含的AD組的用戶。 您是否知道一個具有良好性能的解決方案?如何獲得本地組中的所有用戶(具有良好性能)
2
A
回答
2
您是否嘗試過這個,這個樣本獲取管理員組的成員在本地計算機
using System;
using System.DirectoryServices.AccountManagement;
using System.Collections;
class Program
{
static void Main(string[] args)
{
ArrayList myGroups = GetGroupMembers("Administrators");
foreach (string item in myGroups)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
public static ArrayList GetGroupMembers(string sGroupName)
{
ArrayList myItems = new ArrayList();
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oGroupPrincipal.GetMembers();
foreach (Principal oResult in oPrincipalSearchResult)
{
myItems.Add(oResult.Name);
}
return myItems;
}
public static GroupPrincipal GetGroup(string sGroupName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
return oGroupPrincipal;
}
public static PrincipalContext GetPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Machine);
return oPrincipalContext;
}
}
0
嘿,我做了一組類來做到這一點而回,但它得到了他們基於域而不是組:) :)
這裏是類。有一個userManager類和一個用戶類
public class UserManager
{
private string _domainName;
private Dictionary<string, User> _userLookup;
private PrincipalContext domainContext;
private DirectoryEntry LDAPdirectory;
public UserManager(string domainName)
{
_domainName = domainName;
_userLookup = new Dictionary<string, User>();
domainContext = new PrincipalContext(ContextType.Domain, _domainName);
//Make the LDAP directory look for all users within the domain. DC Com, Au for australia
LDAPdirectory = new DirectoryEntry("LDAP://DC=" + _domainName.ToLower() + ",DC=com,DC=au");
LDAPdirectory.AuthenticationType = AuthenticationTypes.Secure;
}
public IEnumerable<User> Users
{
get
{
return _userLookup.Values.ToArray<User>();
}
set
{
_userLookup.Clear();
foreach (var user in value)
{
if (!_userLookup.ContainsKey(user.Login))
_userLookup.Add(user.Login, user);
}
}
}
/// <summary>
/// Gets all the users from the AD domain and adds them to the Users property. Returns the list.
/// </summary>
/// <returns></returns>
public IEnumerable<User> UpdateAllUsers()
{
DirectorySearcher searcher = new DirectorySearcher(LDAPdirectory);
searcher.Filter = "(&(&(objectClass=user)(objectClass=person)(!objectClass=computer)(objectClass=organizationalPerson)(memberof=*)(telephonenumber=*)))";
SearchResultCollection src = searcher.FindAll();
_userLookup.Clear();
foreach (SearchResult result in src)
{
User newUser = new User(domainContext, result.Properties["samaccountname"][0].ToString());
if (newUser.IsInitialized)
{
_userLookup.Add(newUser.Login, newUser);
yield return newUser;
}
}
}
public User GetUser(string userLogin)
{
return new User(domainContext, userLogin);
}
public bool HasUser(string login)
{
return _userLookup.ContainsKey(login);
}
}
public class User
{
public User()
{
IsInitialized = false;
}
/// <summary>
/// Initializes a new user based on the AD info stored in the domain
/// </summary>
/// <param name="domainContext">The domain to search for this user</param>
/// <param name="userName">The user to look for</param>
public User(PrincipalContext domainContext, string userName)
{
try
{
using (UserPrincipal thisUserPrincipal = UserPrincipal.FindByIdentity(domainContext, userName))
{
this.FirstName = thisUserPrincipal.GivenName;
this.Surname = thisUserPrincipal.Surname;
this.DisplayName = thisUserPrincipal.DisplayName;
this.Email = thisUserPrincipal.EmailAddress;
this.ContactNumber = thisUserPrincipal.VoiceTelephoneNumber;
this.Login = thisUserPrincipal.SamAccountName;
IsInitialized = true;
}
}
catch (Exception)
{
IsInitialized = false;
return;
}
}
/// <summary>
/// Gets a value determining if this user was properly initialized or if an exception was thrown during creation
/// </summary>
public bool IsInitialized { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string Login { get; set; }
public string ContactNumber { get; set; }
}
-1
你可以使用powershell來解決這個問題。
function GetLocalArray{
[CmdletBinding()]
param(
Parameter(ValueFromPipeline=$True,position=0,mandatory=$true)]$ComputerArray
)
BEGIN{
[email protected]()
[email protected]()
}
PROCESS{
foreach($computer in $ComputerArray){
$gwmiquery = Get-WMIObject win32_group -filter "LocalAccount='True'" -computername $computer #| where{$_.Name -like "*Administrators"} #uncomment to filter the result to just local administrators group
$gwmiquery | foreach{
$name=$_.Name;
$A=$_.GetRelated("Win32_UserAccount").Name -join ";"; #users
$B=$_.GetRelated("Win32_Account").Name -join ";"; #systemgroup
$memberUsers = New-Object PSObject -Property @{ComputerName=$_.PSComputerName;Name=$name;AllMembers=$B;Members=$A}
$members+=$memberUsers;
}
}
$filter+= $members | foreach{$name=$_.name; if(! [String]::IsNullOrWhiteSpace($_.AllMembers)) { $_ } }
}
END{
return $filter
}
}
閱讀計算機免受這樣的文件:
$computers = Get-Content $Global:ComputersFile
$AllComputerMembers = GetLocalArray -ComputerArray $computers
從這裏也可以使用,的ConvertTo-CSV,出口CSV,的ConvertTo-JSON 把它用在網頁或Excel。
相關問題
- 1. 具有良好性能的多地圖
- 2. 如何獲取本地用戶所屬的所有本地組
- 3. 具有良好GUI的本地語言
- 4. 是否有任何具有良好性能的泛型TypeConvertor?
- 5. ASP.NET - 使用具有良好性能的SQL數據庫進行本地化
- 6. 如何獲得具有更好性能的重複值的行
- 7. 如何查詢多個COUNT(*)具有良好的性能
- 8. 如何從多個數組中獲得所有可能性?
- 9. 具有良好性能的陣列中的採樣值
- 10. MVC3電網具有良好的功能
- 11. 使用具有良好性能的Aspose創建pdf文件
- 12. 如何從磁帶獲得良好的讀取性能?
- 13. 使用DirectoryEntry獲取本地組中的所有用戶LINQ C#
- 14. Wysiwyg具有良好的API
- 15. 如何獲得具有相同值的所有數組鍵
- 16. 獲得所有組從用戶 - sfDoctrineGuardPlugin
- 17. 如何獲得具有特殊性能的GET-ADUser便有
- 18. 如何從Kinvey獲得所有用戶
- 19. 具有良好用戶界面的報告/儀表板工具
- 20. 在jquery中,如何獲得數組中所有可能的和?
- 21. 保持良好的性能與MySQL數據庫,獲得更多的用戶
- 22. 如何獲得所有廣告組的特定用戶
- 23. 如何有效地獲得一組所有對?
- 24. 如何構建具有良好性能的實時推薦引擎?
- 25. 如何獲得多維數組的所有可能組合
- 26. 如何獲得具有所選屬性的選項值
- 27. 獲得具有特定性能
- 28. 尋找具有良好可用性的應用程序
- 29. 如何獲得來自maven版本組中的所有工件?
- 30. Oracle查詢以獲得所有具有角色和技能的用戶