确定计算机是否在 AD 组中 [英] Determine if computer is in AD group

查看:40
本文介绍了确定计算机是否在 AD 组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定一台计算机是否是 AD 组的成员,或者甚至使用 VB.net 获取属于 AD 组的所有计算机.我找到了几个用于检查用户是否是组成员的示例,但没有用于检查计算机的示例.本来想把这个功能转成群里搜索电脑的,没成功.任何援助将不胜感激.提前致谢.

I am looking to determine if a computer is a member of an AD group or even getting all computers that are members of an AD group using VB.net. I have found several examples for checking to see if a user is a member of a group but none for checking for computers. I was hoping to convert this function to search for a computer in the group but I was unsuccessful. Any assistance would be appreciated. Thank you in advance.

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

推荐答案

如果您正在寻找 CURRENT PC,那么它会更容易一些.

If you're looking for the CURRENT PC then it's a bit easier.

    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

这篇关于确定计算机是否在 AD 组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆