JSI Tip 7700. What users have logon hours configured in my domain?

Jerold Schulman

January 27, 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 LogonHours.bat to display the sAMAccountName and configured logon hours, using the following syntax:

"","

Sample output for user test might look like:

"test","Sunday","05:00","15:00""test","Sunday","21:00","23:00""test","Monday","02:00","23:00""test","Tuesday","02:00","23:00""test","Wednesday","02:00","23:00""test","Thursday","02:00","23:00""test","Friday","02:00","23:00""test","Saturday","02:00","21:00"

NOTE: Only users with configured logon hours are displayed.

The syntax for using LogonHours.bat is:

LogonHours

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

LogonHours>FileName

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

for /f "Tokens=1-4 Delims=," %%i in ('LogonHours') do SomeCommand %%i %%j %%k %%l

LogonHours.bat contains:

@echo offsetlocal ENABLEDELAYEDEXPANSIONset query=dsquery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr sAMAccountName logonHours -limit 0for /f "Tokens=*" %%u in ('%query%') do set line=%%u&call :parseendlocalexit /b 0:parseif /i "%line:~0,14%" NEQ "sAMAccountName" goto detailset /a pos=14:loopset /a pos=%pos% + 1call set work=%%line:~%pos%^,11%%if /i "%work%" NEQ " logonHours" goto :loopset /a pos=%pos% + 1set /a len=%pos% - 2goto :EOF:detailcall set lc=%%line:~%pos%%%if "%lc%" EQU "0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff   " goto :EOFif "%lc%" EQU "                                                                                 " goto :EOFcall set sam="%%line:~0,%len%%%"set sam=%sam:   =%set sam=%sam:  =%set sam=%sam: "="%set hf=Nfor /f "Skip=20 Tokens=*" %%h in ('net user %sam% /domain') do set line=%%h&call :hoursgoto :EOF:hoursif /i "%line:~0,29%" EQU "Logon hours allowed          " set line=%line:~29%&set hf=Yif "%hf%" EQU "N" goto :EOFif /i "%line:~0,23%" EQU "Local Group Memberships" set hf=N&goto :EOFfor /f "Tokens=1-3 Delims=- " %%a in ('@echo !line!')  do (  set day=%%a  set from=%%b  set to=%%c@echo %sam%,"!day!","!from!","!to!")



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