【问题标题】:Vb.Net Get Domain Member Computer Enabled/Disabled StatusVb.Net 获取域成员计算机启用/禁用状态
【发布时间】:2014-12-14 12:09:37
【问题描述】:

我对 Vb.net 和 Active Directory 对象检索还很陌生。我已经编译了以下内容,它在网格视图中获取了计算机、用户详细信息。我在尝试检索计算机的启用/禁用状态时咬紧牙关,这是我们域的成员。请帮忙。代码块在下面给出。

注意:对于那些被禁用的计算机,我能够将“userAccountControl”属性值检索到 4098。我想知道是否可以实施更好的方法。

 Public Sub GetPropertiesDisplyinForm()
    Dim rootDse As New DirectoryEntry("LDAP://rootDSE")
    Dim direntry As New DirectoryEntry("LDAP://" + DirectCast(rootDse.Properties("defaultNamingContext").Value, String))

    Dim dn As String
    Dim dn1 As String
    Dim dn2 As String
    Dim dn3 As String
    Dim dn4 As String
    Dim dn5 As String
    Dim dn6 As String
    Dim dn7 As String

    Dim AcctDisable As Boolean


    Try

        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(direntry)

        If User_Computer = "Computers" Then
            ' For the list of computers

            dirSearcher.PropertiesToLoad.Add("cn")
            dirSearcher.PropertiesToLoad.Add("SamAccountName")
            dirSearcher.PropertiesToLoad.Add("DisplayName")
            dirSearcher.PropertiesToLoad.Add("userAccountControl")
            dirSearcher.PropertiesToLoad.Add("LastLogon")
            dirSearcher.PropertiesToLoad.Add("distinguishedName")
            dirSearcher.PropertiesToLoad.Add("description")
            dirSearcher.PropertiesToLoad.Add("info")

            dirSearcher.Filter = ("(objectClass=computer)")

            ' Draw the GridView with columns
            DataGridView1.Columns.Add("sl", "sl")
            DataGridView1.Columns.Add("cn", "Computer")
            DataGridView1.Columns.Add("SamAccountName", "SamAccountName")
            DataGridView1.Columns.Add("DisplayName", "DisplayName")
            DataGridView1.Columns.Add("Enabled", "Enabled")
            DataGridView1.Columns.Add("LastLogon", "LastLogon")
            DataGridView1.Columns.Add("distinguishedName", "distinguishedName")
            DataGridView1.Columns.Add("description", "description")
            DataGridView1.Columns.Add("info", "info")

        ElseIf User_Computer = "Users" Then
            ' For the list of Users
            dirSearcher.PropertiesToLoad.Add("cn")
            dirSearcher.PropertiesToLoad.Add("sAMAccountName")
            dirSearcher.PropertiesToLoad.Add("employeeNumber")
            dirSearcher.PropertiesToLoad.Add("displayName")
            dirSearcher.PropertiesToLoad.Add("mail")

            dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person))"

            ' For Users
            DataGridView1.Columns.Add("sl", "sl")
            DataGridView1.Columns.Add("cn", "cn")
            DataGridView1.Columns.Add("sAMAccountName", "User Name")
            DataGridView1.Columns.Add("employeeNumber", "Employee Number")
            DataGridView1.Columns.Add("displayName", "Full Name")
            DataGridView1.Columns.Add("mail", "E-Mail Address")
        End If

        Dim dirSearchResults As SearchResult ' Search results are stored here

        If User_Computer = "Computers" Then

            objCounter = 0


            For Each dirSearchResults In dirSearcher.FindAll()
                objCounter = objCounter + 1

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("SamAccountName")
                    dn = pt
                Next

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("DisplayName")
                    dn1 = pt
                Next

                Dim ptdummy As Integer = CInt(dirSearchResults.GetDirectoryEntry.Properties("userAccountControl").Value)
                dn2 = ptdummy.ToString

                For Each pt As ActiveDs.LargeInteger In dirSearchResults.GetDirectoryEntry.Properties("LastLogon")
                    Dim lngHigh As Long = pt.HighPart
                    Dim lngLow As Long = pt.LowPart
                    dn3 = FormatDateTime((DateTime.FromFileTime((lngHigh * (2 ^ 32) - lngLow))))

                Next

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("distinguishedName")
                    dn4 = pt
                Next
                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("info")
                    dn5 = pt
                Next
                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("description")
                    dn6 = pt
                Next

                Dim rt() As String = New String() {objCounter, dirSearchResults.GetDirectoryEntry.Properties("cn")(0), dn, dn1, dn2, dn3, dn4, dn5, dn6}
                DataGridView1.Rows.Add(rt)
                dn = ""
                dn1 = ""
                dn2 = ""


            Next

            Me.Label4.Visible = True
            Me.Label4.Text = "Object Count:" & DataGridView1.Rows.Count.ToString

            If objCounter > 0 Then
                Me.ButtonExpExcel.Enabled = True

            End If

        ElseIf User_Computer = "Users" Then
            objCounter = 0

            For Each dirSearchResults In dirSearcher.FindAll()
                objCounter = objCounter + 1

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("sAMAccountName")
                    dn = pt
                Next

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("employeeNumber")
                    dn1 = pt
                Next

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("displayName")
                    dn2 = pt
                Next

                For Each pt As String In dirSearchResults.GetDirectoryEntry.Properties("mail")
                    dn3 = pt
                Next

                Dim rt() As String = New String() {objCounter, dirSearchResults.GetDirectoryEntry.Properties("cn")(0), dn, dn1, dn2, dn3}

                DataGridView1.Rows.Add(rt)

                dn = ""
                dn1 = ""
                dn2 = ""
                dn3 = ""


            Next


            Me.Label4.Visible = True
            Me.Label4.Text = "Object Count:" & DataGridView1.Rows.Count.ToString



            If objCounter > 0 Then
                Me.ButtonExpExcel.Enabled = True

            End If

        End If


    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

【问题讨论】:

    标签: .net vb.net active-directory


    【解决方案1】:

    我能够通过跟踪线程找出域成员计算机和用户的启用或禁用状态 http://www.vbforums.com/showthread.php?599951-RESOLVED-Check-if-account-is-disabled-Active-Directory

    示例代码如下

    Try
                    Const ADS_UF_ACCOUNTDISABLE As Integer = &H2 ' new
    
                    Dim ptdummy As Integer = CInt(dirSearchResults.GetDirectoryEntry.Properties("userAccountControl").Value)
                    'If ptdummy = 4098 Then
                    '    dn3 = "Disabled"
                    'Else
                    '    dn3 = "Enabled"
                    'End If
    
                    If CBool(ptdummy And ADS_UF_ACCOUNTDISABLE) Then
                        dn3 = "Disabled"
                    Else
                        dn3 = "Enabled"
                    End If
                Catch
                    dn3 = Nothing
                End Try
    

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 2022-11-02
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多