Seite 1 von 1

Resolve SID to local group name

Verfasst: So 10. Mai 2020, 22:51
von Sebastian

Code: Alles auswählen

Imports System.DirectoryServices

[...]

    Public Function GetGroupNameBySID(ByVal strSID As String) As String
        Dim strReturnValue As String = Nothing

        Using SearchRoot As New DirectoryEntry
            Using LocalSearcher As New DirectorySearcher(SearchRoot)
                LocalSearcher.SearchScope = SearchScope.Subtree
                LocalSearcher.Filter = "(objectSid=" & strSID & ")"
                LocalSearcher.PropertiesToLoad.Add("objectSid")
                LocalSearcher.PropertiesToLoad.Add("sAMAccountName")


                Dim searchresultLocalGroup As SearchResult = LocalSearcher.FindOne
                If Not searchresultLocalGroup Is Nothing Then
                    strReturnValue = searchresultLocalGroup.Properties("sAMAccountName").Item(0).ToString()
                End If
            End Using
        End Using

        Return strReturnValue
    End Function