Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DirecotoryServices - Find all Windows 7 and Windows 10 Computers in Domain

DirecotoryServices - Find all Windows 7 and Windows 10 Computers in Domain

Scheduled Pinned Locked Moved Visual Basic
csharpsysadminwindows-adminhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11833437
    wrote on last edited by
    #1

    I am creating a tool to delete user profiles by age on all windows 7 and windows 10 devices on our domains. I have it working using this filter ("(objectClass=computer)") but want to refine that to only get windows 7 and windows 10 devices because my department manages desktops/laptops not servers. In order to make the tool more efficient and not attempt to connect to devices I don't have admin access to. So I tried this filter... mySearcher.Filter = "(&(objectClass=computer)(operatingSystemVersion=*server*))" my thought was to do something like this if OS IsNot Windows Server 2012 or Windows Server 2008 then do something. I loaded the operatingSystem property but can't get it to display. I have tried it in the datagridview and when that didn't work I just tried to do a consolewriteline and that didn't work either. I want to test my concept before adding the code to use WMI to delete profiles by just displaying the hostname and OS version to make sure it is pulling the information from AD and not giving me servers. Can someone help? Here is the code(I am just working with the v06 domain at the moment):

    Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
    mySearcher.PropertiesToLoad.Add("dNSHostName")
    mySearcher.PropertiesToLoad.Add("operatingSystem")
    mySearcher.PropertiesToLoad.Add("operatingSystemVersion")
    mySearcher.Filter = "(&(objectClass=computer)(operatingSystem=*server*))" '("(objectClass=computer)")

        Dim resEnt As SearchResult
        For Each resEnt In mySearcher.FindAll()
            Select Case dMain
                Case "v06"
                    Try
    
                        hostName = GetProperty(resEnt, "dNSHostName")
                        OSystem = GetProperty(resEnt, "operatingSystem")
                        Dim result1 As Net.NetworkInformation.PingReply = ping.Send(hostName, 1000)
                        If result1.Status = Net.NetworkInformation.IPStatus.Success Then
                            Dim row2 As String() = New String() {hostName, "N/A", OSystem}
                            dgvLocal.Rows.Add(row2)
                        Else
                            My.Computer.FileSystem.WriteAllText("C:\\ExodusErrorLog.txt", hostName & " - Device is not pingable!" & vbCrLf & vbCrLf, True)
                        End If
                    Catch ex As Exception
                        My.Computer.FileSystem.WriteAllText("C:\\ExodusErrorLog.txt", hostName & " - " & ex.Message & vbCrLf & vbCrLf, True)
    
    L 1 Reply Last reply
    0
    • U User 11833437

      I am creating a tool to delete user profiles by age on all windows 7 and windows 10 devices on our domains. I have it working using this filter ("(objectClass=computer)") but want to refine that to only get windows 7 and windows 10 devices because my department manages desktops/laptops not servers. In order to make the tool more efficient and not attempt to connect to devices I don't have admin access to. So I tried this filter... mySearcher.Filter = "(&(objectClass=computer)(operatingSystemVersion=*server*))" my thought was to do something like this if OS IsNot Windows Server 2012 or Windows Server 2008 then do something. I loaded the operatingSystem property but can't get it to display. I have tried it in the datagridview and when that didn't work I just tried to do a consolewriteline and that didn't work either. I want to test my concept before adding the code to use WMI to delete profiles by just displaying the hostname and OS version to make sure it is pulling the information from AD and not giving me servers. Can someone help? Here is the code(I am just working with the v06 domain at the moment):

      Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
      mySearcher.PropertiesToLoad.Add("dNSHostName")
      mySearcher.PropertiesToLoad.Add("operatingSystem")
      mySearcher.PropertiesToLoad.Add("operatingSystemVersion")
      mySearcher.Filter = "(&(objectClass=computer)(operatingSystem=*server*))" '("(objectClass=computer)")

          Dim resEnt As SearchResult
          For Each resEnt In mySearcher.FindAll()
              Select Case dMain
                  Case "v06"
                      Try
      
                          hostName = GetProperty(resEnt, "dNSHostName")
                          OSystem = GetProperty(resEnt, "operatingSystem")
                          Dim result1 As Net.NetworkInformation.PingReply = ping.Send(hostName, 1000)
                          If result1.Status = Net.NetworkInformation.IPStatus.Success Then
                              Dim row2 As String() = New String() {hostName, "N/A", OSystem}
                              dgvLocal.Rows.Add(row2)
                          Else
                              My.Computer.FileSystem.WriteAllText("C:\\ExodusErrorLog.txt", hostName & " - Device is not pingable!" & vbCrLf & vbCrLf, True)
                          End If
                      Catch ex As Exception
                          My.Computer.FileSystem.WriteAllText("C:\\ExodusErrorLog.txt", hostName & " - " & ex.Message & vbCrLf & vbCrLf, True)
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Here's an alternative to the solution you seek; get a full list of all the PC's, and then use LINQ to query out the ones that are servers. You may also get a better query from a network-admin; it is querying the AD after all.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups