Function SearchPassNeverExpiresUsers()
' Description: Searches all users in the domain with "Password Never Expires" set
' Returns: an array with all the users
Dim objRootDSE, objConnection, objCommand, objRecordSet, objUser
Dim OutPut
Set objRootDSE = GetObject("LDAP://rootDSE")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "<LDAP://" & objRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=person)(objectClass=user)" & _
"(userAccountControl:1.2.840.113556.1.4.803:=65536))" & _
";distinguishedName,sAMAccountName;subtree"
Set objRecordSet = objCommand.Execute
ReDim OutPut(0)
While Not objRecordSet.EOF[COLOR=blue]
' On Error Resume next
Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName") & "")
'Do a binary comparison to see if account is disabled
If Not objUser.get("userAccountControl") And 2 Then[/color]
If OutPut(0) <> "" Then ReDim Preserve OutPut(UBound(OutPut) + 1)
OutPut(UBound(OutPut)) = objRecordSet.Fields("sAMAccountName")
[COLOR=blue]End If
' On Error GoTo 0
Set objUser = Nothing[/color]
objRecordSet.MoveNext
Wend
objConnection.Close
SearchPassNeverExpiresUsers = OutPut
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRootDSE = Nothing
End Function