Skip navigation

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

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:

"<sAMAccountName>","<Allowed Hours>

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 off
setlocal ENABLEDELAYEDEXPANSION
set query=dsquery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr sAMAccountName logonHours -limit 0
for /f "Tokens=*" %%u in ('%query%') do set line=%%u&call :parse
endlocal
exit /b 0
:parse
if /i "%line:~0,14%" NEQ "sAMAccountName" goto detail
set /a pos=14
:loop
set /a pos=%pos% + 1
call set work=%%line:~%pos%^,11%%
if /i "%work%" NEQ " logonHours" goto :loop
set /a pos=%pos% + 1
set /a len=%pos% - 2
goto :EOF
:detail
call 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 :EOF
if "%lc%" EQU "                                                                                 " goto :EOF
call set sam="%%line:~0,%len%%%"
set sam=%sam:   =%
set sam=%sam:  =%
set sam=%sam: "="%
set hf=N
for /f "Skip=20 Tokens=*" %%h in ('net user %sam% /domain') do set line=%%h&call :hours
goto :EOF
:hours
if /i "%line:~0,29%" EQU "Logon hours allowed          " set line=%line:~29%&set hf=Y
if "%hf%" EQU "N" goto :EOF
if /i "%line:~0,23%" EQU "Local Group Memberships" set hf=N&goto :EOF
for /f "Tokens=1-3 Delims=- " %%a in ('@echo !line!')  do (
  set day=%%a
  set from=%%b
  set to=%%c
@echo %sam%,"!day!","!from!","!to!"
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish