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

  • Thread starter Thread starter Chet.Horton
  • Start date Start date
C

Chet.Horton

Guest

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, "operatingSystemVersion")
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)
End Try
Case "v07.med.va.gov"
End Select
Next




Continue reading...
 
Back
Top