title: Vb.Net Get Domain Member Computer Enabled/Disabled Status
body: <p>I'm pretty new to Vb.net and Active directory objects retrieval. I've compiled the following, that fetches me both Computer, User details in a grid view. I have bite the bullet while trying to retrieve the Enabled/Disabled status of a computer, that is a member of our domain. Please help. The code chunk is give below. </p>
  
  <p>Note: I was able to retrieve "userAccountControl" attribute value to 4098 for the computers those are disabled. I would like to know whether a better approach could be implemented.</p>
  
  <pre><code> 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 = "(&amp;(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:" &amp; DataGridView1.Rows.Count.ToString
  
              If objCounter &gt; 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:" &amp; DataGridView1.Rows.Count.ToString
  
  
  
              If objCounter &gt; 0 Then
                  Me.ButtonExpExcel.Enabled = True
  
              End If
  
          End If
  
  
      Catch ex As Exception
          MessageBox.Show(ex.Message)
      End Try
  End Sub
  </code></pre>