Solved

List users not logged in for longer time

Posted on 2011-03-14
4
526 Views
Last Modified: 2012-05-11
Hi there,

I have an environment with users only working for a couple of weeks or months.
Every new user gets a new account.
After leaving the unit, the accounts used to remain untouched.
The accounts are blocked with a policy after 30 days.

Now we have the need to clean up about 250 user accounts not longer valid any more.

Is there a method to list user accounts with criteria like
- Password/Login blocked (by policy)
- last login longer than a given time

thanks for your help

Michael
0
Comment
Question by:michaellandwehr
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Ernie Beek earned 500 total points
ID: 35127161
Well, I have a script that creates a csv file with the last logon of users. Perhaps that might help you.

 
Const FILE_OUT = "LastLogon.txt"
' Get time zone adjustment. If GMT is good enough, you can omit all this bias-related logic.
Set oShell = CreateObject("Wscript.Shell")
oBias = oShell.RegRead("HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
If VarType(oBias) = vbLong Then
    lBias = oBias
ElseIf VarType(oBias) = vbVariant Then
    lBias = 0
    For iByte = 0 To UBound(oBias)
        lBias = lBias * 256 + oBias(iByte)
    Next
End If
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFile = oFS.CreateTextFile(FILE_OUT, True)
oFile.WriteLine "distinguishedName" & vbTab & "LastLogon"
Set cnAD = CreateObject("ADODB.Connection")
cnAD.Provider = "ADsDSOObject"
cnAD.Open "Active Directory Provider"
Set cmdQry = CreateObject("ADODB.Command")
Set cmdQry.ActiveConnection = cnAD
Set oRootDSE = GetObject("LDAP://RootDSE")
cmdQry.CommandText = "<LDAP://" & oRootDSE.Get("defaultNamingContext") & ">;(objectCategory=person);distinguishedName;subtree"
cmdQry.Properties("Page Size") = 500
Set rsUsers = cmdQry.Execute()
Do While Not rsUsers.EOF
    sDN = rsUsers("distinguishedName")
    Set oUser = GetObject("LDAP://" & sDN)
    sLastLogon = ""
    On Error Resume Next
    Set oLastLogon = oUser.LastLogon
    If Err = 0 Then
        lHigh = oLastLogon.HighPart
        lLow = oLastLogon.LowPart
        If lLow = 0 And lHigh = 0 Then
        Else
            If lLow < 0 Then
                lHigh = lHigh + 1 ' compensates for API quirk
            End If
            sLastLogon = FormatDateTime((#1/1/1601# + ((lHigh * 2 ^ 32 + lLow) / 600000000 - lBias) / 1440),2)
        End If
    End If
    On Error GoTo 0
    oFile.WriteLine sDN & vbTab & sLastLogon
    rsUsers.MoveNext
Loop
rsUsers.Close
cnAD.Close
oFile.Close

Open in new window

0
 
LVL 9

Expert Comment

by:Chev_PCN
ID: 35127840
Use ADFIND.

adfind -f "&(objectcategory=user)(lockouttime>=1)" -dn >>c:\a\lockout.txt
0
 

Author Comment

by:michaellandwehr
ID: 35128556
Hi erniebeek,

Your script solved my problem so far.
I only had to modify lline 43 as it was easier to sort on the date on the first position and to replace  vbTab with "," ( the  vbTab variable did not work)


    oFile.WriteLine sLastLogon & "," & sDN


Thank you very much.

Michael
0
 
LVL 35

Expert Comment

by:Ernie Beek
ID: 35129279
Glad it helped you out :)

And thx for the points.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now