2010-05-07 48 views
1

我試圖遠程列出本地管理員組的成員。以下代碼僅返回屬於管理員組的本地帳戶 - 根本不返回域組或個人帳戶(例如BLAH \ Domain Admins或BLAH \ yajohn)。使用System.DirectoryServices.AccountManagement列出本地管理員不檢索域用戶

任何人有想法?

 Public Function listLocalAdmins(ByVal machinename As String, ByVal creduname As String, ByVal credpass As String) As String 
    Try 
     Dim mctx As New PrincipalContext(ContextType.Machine, machinename, creduname, credpass) 
     Dim lcladmins As GroupPrincipal = GroupPrincipal.FindByIdentity(mctx, IdentityType.Name, "Administrators") 
     Dim pc As PrincipalCollection = lcladmins.Members 
     Dim r As New StringBuilder 
     For Each p As Principal In pc 
      r.Append("Name:->" & p.Name.ToString & vbCrLf) 
     Next 
     Return r.ToString 
    Catch ex As Exception 
     Return ex.Message 
    End Try 
End Function 

感謝您的任何反饋。

回答

0

我之前發佈過,但發現它沒有解決您的問題。我無法使用AccountManagement來做你想做的事。儘管我可以使用DirectoryServices,但這可能會有所幫助。

Imports System.DirectoryServices 


Sub Main() 
    'basic props' 
    Dim computername As String = "computername" 
    Dim username As String = "Domain1\account" 
    Dim password As String = "password" 

    'User to check if they are part of ADMIN group' 
    Dim userToCheck As String = "usertocheck" 

    'User to add/remove' 
    Dim usertoAddRemove As String = "usertoaddremove" 

    'get computer entry' 
    Dim deComputer As DirectoryEntry = GetComputerEntry(computername, username, password) 

    'get admin group info' 
    Dim deGroup As DirectoryEntry = GetGroupByName(deComputer, "administrators") 

    'get members' 
    Dim groupMembers As List(Of DirectoryEntry) = GetGroupMembers(deGroup) 

    'check if "UserToCheck" is part of admin group' 
    Console.WriteLine(String.Format("User {0} Found?: {1}", userToCheck, CheckIfUsernameIsInGroup(deGroup, userToCheck).ToString())) 

    'get user to add/remove DN' 
    Dim userDN As DirectoryEntry = New DirectoryEntry(String.Format("WinNT://{0}/{1},user", "DOMAIN1", usertoAddRemove)) 

    'add account' 
    AddUserToGroup(deGroup, userDN) 
    Console.WriteLine(String.Format("User account {0} added to group {1}", usertoAddRemove, deGroup.Name)) 

    'remove account' 
    RemoveUserFromGroup(deGroup, userDN) 
    Console.WriteLine(String.Format("User account {0} removed from group {1}", usertoAddRemove, deGroup.Name)) 

    Console.ReadLine() 

End Sub 

Public Function GetComputerEntry(ByVal Computername As String, ByVal Username As String, ByVal Password As String) As DirectoryEntry 
    'create directory entry connection to the remote machine' 
    Dim deComputer As New DirectoryEntry("WinNT://" + Computername + ",computer", Username, Password) 
    deComputer.RefreshCache() 

    Return deComputer 
End Function 

Public Function GetGroupByName(ByVal DE As DirectoryEntry, ByVal Groupname As String) As DirectoryEntry 
    'get admin group info' 
    Dim deGroup As DirectoryEntry = DE.Children.Find(Groupname, "group") 

    Return deGroup 
End Function 

Public Function GetGroupMembers(ByVal deGroup As DirectoryEntry) As List(Of DirectoryEntry) 
    Dim members As IEnumerable = deGroup.Invoke("members", Nothing) 
    Dim r As New List(Of DirectoryEntry)() 

    For Each o As Object In members 
     Dim deMember As DirectoryEntry = New DirectoryEntry(o) 

     r.Add(deMember) 
    Next 

    Return r 
End Function 

Public Function CheckIfUsernameIsInGroup(ByVal deGroup As DirectoryEntry, ByVal Username As String) As Boolean 
    'first get group members' 
    Dim u As List(Of DirectoryEntry) = GetGroupMembers(deGroup) 

    'then check for name' 
    Dim r = From c In u Where c.Name.ToUpper() = Username.ToUpper() Select c 

    'return true/false if found' 
    Return r.Count = 1 
End Function 

Public Sub AddUserToGroup(ByVal deGroup As DirectoryEntry, ByVal User As DirectoryEntry) 
    deGroup.Invoke("Add", User.Path.ToString()) 
    deGroup.CommitChanges() 
End Sub 

Public Sub RemoveUserFromGroup(ByVal deGroup As DirectoryEntry, ByVal User As DirectoryEntry) 
    deGroup.RefreshCache() 
    deGroup.Invoke("Remove", User.Path.ToString()) 
    deGroup.CommitChanges() 
End Sub 
相關問題