List local administrators with System.DirectoryServices.AccountManagement doesn't retrieve domain us

Posted by yajohn on Stack Overflow See other posts from Stack Overflow or by yajohn
Published on 2010-05-07T22:03:22Z Indexed on 2010/05/07 22:08 UTC
Read the original article Hit count: 458

I'm trying to remotely list members of the local Administrators group. The following code returns only local accounts which are members of the admin group - no domain groups or individual accounts are returned at all (BLAH\Domain Admins or BLAH\yajohn, for instance).

Anyone have an idea?

      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

Thanks for any feedback.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about system.directoryservices