JSI Tip 7694. How many times has a user (or all users) logged onto the domain?

Jerold Schulman

January 25, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Using the Active Directory command-line tools, in a Windows 2000 domain, or Windows Server 2003 domain, I have scripted DomainLogonCount.bat to display the distinguished name and domain logon count, using the semi-colon (;) delineated format of "Distinguished Name";"Logon Count", for one or all users.

The syntax for using DomainLogonCount.bat is:

[call] DomainLogonCount SamId | *

where Samid is the user name, like Jerold, or an asterisk (*) to display all users.

The output is displayed on the CMD console, but you can pipe it to a file using the following syntax:

DomainLogonCount *>FileName

You can use the output in subsequent commands, as in:

for /f "Tokens=1* Delims=;" %%i in ('DomainLogonCount *') do SomeCommand %%i %%j

DomainLogonCount.bat contains:

@echo offif {%1}=={} @echo Syntax [call] DomainLogonCount SamId ^| *&exit /b 1setlocalset user=%1set query=dsquery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=%user%))" -attr distinguishedName logonCount -limit 0for /f "Tokens=*" %%u in ('%query%') do set line=%%u&call :parseendlocalexit /b 0:parseif /i "%line:~0,17%" NEQ "distinguishedName" goto detailset /a pos=17:loopset /a pos=%pos% + 1call set work=%%line:~%pos%^,11%%if /i "%work%" NEQ " logonCount" goto :loopset /a pos=%pos% + 1set /a len=%pos% - 2goto :EOF:detailcall set lc=%%line:~%pos%%%set /a lc=100000%lc%%%100000call set dn="%%line:~0,%len%%%"set dn=%dn:   =%set dn=%dn:  =%set dn=%dn: "="%@echo %dn%;"%lc%"



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like