【问题标题】:Determine if computer is in AD group确定计算机是否在 AD 组中
【发布时间】:2015-08-01 20:05:57
【问题描述】:

我正在寻找确定计算机是否是 AD 组的成员,或者甚至使用 VB.net 获取属于 AD 组成员的所有计算机。我找到了几个用于检查用户是否是组成员的示例,但没有找到用于检查计算机的示例。我希望将此功能转换为在组中搜索计算机,但没有成功。任何援助将不胜感激。提前谢谢你。

Function IsInGroup(GroupName) As Boolean
    Dim MyIdentity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
    Dim MyPrincipal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(MyIdentity)
    Return MyPrincipal.IsInRole(GroupName)
End Function

【问题讨论】:

  • 我找到了这个,但我还不够好,无法将其转换为 vb.net。有人可以帮我吗? Set objSysInfo = CreateObject("ADSystemInfo") strComputer = objSysInfo.ComputerName Set objComputer = GetObject("LDAP://" & strComputer) objmemberOf = objComputer.GetEx("memberOf") For Each objGroup in objmemberOf groupCN = Split(objGroup, ",") groupName = Mid(groupCN(0),4) Next Select Case groupName Case "TestGroup" Wscript.Echo groupName Case Else Wscript.Echo "" End Select

标签: vb.net


【解决方案1】:

试试这样的

Function IsInGroup(PCName As String, groupName As String) As Boolean
      Dim vUsuario As New NTAccount(PCName & "$")
      Dim sid As SecurityIdentifier = vUsuario.Translate(GetType(SecurityIdentifier))

      Using vRootDSE As New DirectoryEntry("LDAP://rootDSE")
         Using vSearcher As New DirectorySearcher(New DirectoryEntry("LDAP://" + CStr(vRootDSE.Properties("defaultNamingContext")(0))), "(objectSID=" & sid.ToString() & ")", New String() {"memberOf"}, SearchScope.Subtree)
            Dim src As SearchResultCollection = vSearcher.FindAll()

            Dim memberOf As ResultPropertyValueCollection = src(0).Properties("memberOf")
            For i As Integer = 0 To memberOf.Count - 1
               'Debug.Print(memberOf(i).ToString())

               ' I don't really like this approach, but it's quick to write ;)
               If memberOf(i).ToString().Contains("=" & groupName & ",") Then
                  Return True
               End If
            Next

         End Using

      End Using

      Return False
   End Function

【讨论】:

    【解决方案2】:

    如果您正在寻找当前的 PC,那么它会更容易一些。

        Function Is_CurrentPC_InADGroup(groupName As String) As Boolean
            if groupName = "" then Return True
            Using context = New PrincipalContext(ContextType.Domain, Environment.GetEnvironmentVariable("USERDOMAIN"))
                Dim principal = ComputerPrincipal.FindByIdentity(context, Environment.MachineName)
                Dim groups = principal.GetGroups()
    
                For Each group In groups
                    If group.ToString = groupName Then Return True
                Next
            End Using
            Return False
        End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 2012-07-07
      • 2018-12-09
      相关资源
      最近更新 更多