StevensNet.com

...Because someone's gotta tell the story...

Blogs

To return to the main Blog List, click Full Blog Listing.

Tags

Active Directory
Error
Troubleshoot
Troubleshooting
VBScript
Visual Basic
Windows

Finding inactive computers in Active Directory (VB6)

Tuesday, July 20, 2010 in Active Directory (Views: 4751)
Here is some test code I was working on, hope this helps. What it does is looks in Active Directory and determines if a computer has been inactive longer than the specified duration.

In this code, I was only looking for workstations, but you can remove the Windows 2000, XP, etc parts of the code to be less discriminating:

You can just test this in a new sub... Make sure to change the domain to your own, and the intduration will (by default be 90 days). You can change this as you like. This could serve as a good starter piece of code to get you going.

This can also be easily adapted to VBScript.

===============================================

Dim i, i2, i3 As Integer intMaxInactive = 1i2 = 0i = 0

On Error Resume NextConst ADS_SCOPE_SUBTREE = 2Dim objConnection, objCommand, objRecordSet, objNewOU, objComputer, objOriComputerDim strDomainDim intDurationstrDomain = "DC=domain,DC=com"intDuration = 90

Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnectionobjCommand.CommandText = "Select Name,DistinguishedName,OperatingSystem from 'LDAP://" & strDomain & "' where objectClass='computer'"objCommand.Properties("Page Size") = 1500objCommand.Properties("Timeout") = 30objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREEobjCommand.Properties("Cache Results") = FalseSet objRecordSet = objCommand.ExecuteIf Err.Number <> 0 ThenDebug.Print "Cmd1 failed: " + Str(Err.Number)End IfSet objOriComputer = GetObject("LDAP://CN=Computers," & strDomain)objRecordSet.MoveFirstDim dtmValue, intDateDiffDo While Not objRecordSet.EOFi = i + 1Set objComputer = GetObject("LDAP://" & objRecordSet.Fields("DistinguishedName").Value)dtmValue = CDate(objComputer.PasswordLastChanged)

Select Case TrueCase InStr(objRecordSet.Fields("OperatingSystem").Value, "Windows XP") <> 0, _InStr(objRecordSet.Fields("OperatingSystem").Value, "Windows 7") <> 0, _InStr(objRecordSet.Fields("OperatingSystem").Value, "Windows 2000") <> 0, _InStr(objRecordSet.Fields("OperatingSystem").Value, "Windows Vista") <> 0 DoEvents intDateDiff = CInt(Now - dtmValue) If CInt(intDateDiff) > intDuration Then i2 = i2 + 1 ReDim Preserve strInactive(i2) Debug.Print "Computer: " + objRecordSet.Fields("Name").Value + " inactive " + LTrim(RTrim(Str(intDateDiff))) + " days." + Str(intMaxInactive) + " " + objRecordSet.Fields("OperatingSystem").Value If intDateDiff > intMaxInactive Then intMaxInactive = intDateDiff Debug.Print "New winner: " + objRecordSet.Fields("Name").Value + " - Days: " + LTrim(RTrim(Str(intDateDiff))) + " " + objRecordSet.Fields("OperatingSystem").Value End If End IfEnd Select

objRecordSet.MoveNextErr.ClearLoop

Debug.Print "Computers Processed: " + Str(i)Debug.Print "Computers over " + Str(intDuration) + " days: " + Str(i2)intInactive = i2

 

Related Blogs You May Be Interested In:


To leave a comment, please log in and/or register.