Vb.Net获取域成员计算机启用/禁用状态

时间:2014-12-14 12:09:38

标签: .net vb.net active-directory

我对Vb.net和Active目录对象检索很新。我编译了以下内容,它在网格视图中提取了计算机,用户详细信息。我试图检索计算机的启用/禁用状态时咬了一口,这是我们域的成员。请帮忙。代码块在下面给出。

注意:我能够为禁用的计算机检索“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

1 个答案:

答案 0 :(得分:0)

我可以通过关注该线程来确定域成员计算机和用户的启用或禁用状态 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