Here's one I cooked up for my Home Domain; It shuts down Windows computers if there is no one logged into the console. This is run as a scheduled task on the DC right after the kid's bedtime (actually three times per night, but that is for another day) . The Domain Logon hours for the kids force a logoff, so this script is intended shutdown computers which do not have a console user logged-on. The Script will not shutdown Windows Server Operating systems.
Code:
@ECHO OFF ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Script to Shut Down Idle PCs After Hours. :: :: Script will not shutdown PCs on the 2nd Tue. or Wed. of the Month. :: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: SETLOCAL SET BIN_PATH=C:\Tools\ SET DAY_DATE=UNKNOWN SET DEVICE_NAME=UNKNOWN SET LOCAL_DOMAIN=UNKNOWN SET LOG_FILE=Domain_Shutdown.log SET LOG_PATH=C:\TEMP SET SHUTDOWN_LOG=%LOG_PATH%\%LOG_FILE% SET STATUS=UNKNOWN :GET_DOMAIN FOR /F "tokens=1,2 delims== usebackq" %%a IN (`WMIC COMPUTERSYSTEM GET Domain /FORMAT:CSV`) DO FOR /F "tokens=2 delims=, usebackq" %%b IN (`ECHO %%a`) DO FOR /F "tokens=1 delims=. usebackq" %%c IN (`ECHO %%b`) DO SET LOCAL_DOMAIN=%%c ECHO.>>%SHUTDOWN_LOG%& ECHO Domain is %LOCAL_DOMAIN%.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG% :CHECK_DAY FOR /F "tokens=1,2 usebackq" %%a IN (`ECHO %DATE% /FIND /I "tue"`) DO FOR /F "tokens=1,2,3 delims=/- usebackq" %%c IN (`ECHO %%b`) DO SET DAY_DATE=%%d FOR /F "tokens=1,2 usebackq" %%a IN (`ECHO %DATE% /FIND /I "wed"`) DO FOR /F "tokens=1,2,3 delims=/- usebackq" %%c IN (`ECHO %%b`) DO SET DAY_DATE=%%d IF %DAY_DATE% LEQ 7 ECHO.>>%SHUTDOWN_LOG%& ECHO %DATE% is Restricted from Shutdown.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO LOG_RESULTS ECHO.>>%SHUTDOWN_LOG%& ECHO %DATE% is Not Restricted from Shutdown.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG% :CHECK_CLIENTS FOR /F "tokens=1 usebackq" %%a IN (`NET VIEW ^| FIND /I "\\"`) DO CALL :PARSE_DEVICE_NAME %%a :LOG_RESULTS ECHO.>>%SHUTDOWN_LOG%& ECHO END OF SCRIPT>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG% ENDLOCAL GOTO END :PARSE_DEVICE_NAME SET DEVICE_NAME=%1 SET DEVICE_NAME=%DEVICE_NAME:~2% ECHO.>>%SHUTDOWN_LOG%& ECHO DEVICE NAME=%DEVICE_NAME%.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG% ::DEVICE-CONNECTIVITY-CHECK FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "time"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=ONLINE>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO SERVER-EXCEPTION-CHECK FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "host"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=OFFLINE>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "find"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=UNKNOWN>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END :SERVER-EXCEPTION-CHECK FOR /F "tokens=* usebackq" %%a IN (`WMIC /NODE:%DEVICE_NAME% OS GET Name ^| FIND /I "server"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO Server OS Detected, no Shutdown attempted.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END ::CONSOLE-USER-CHECK FOR /F "tokens=1,2 delims=\ usebackq" %%a IN (`WMIC /NODE:%DEVICE_NAME% COMPUTERSYSTEM GET UserName ^| FIND /I "%LOCAL_DOMAIN%"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO Console User %%b Detected, no Shutdown attempted.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END :SHUTDOWN_PC ::PAYLOAD ECHO.>>%SHUTDOWN_LOG%& ECHO Executing the following Command Line: PSSHUTDOWN.exe -s -f -t 10 \\%DEVICE_NAME%>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG% "%BIN_PATH%\PSSHUTDOWN.exe" -s -f -t 10 \\%DEVICE_NAME%>>%SHUTDOWN_LOG% :END
Comment