Self Logging Batch Script

@echo off

set datestamp=%date:~-4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%%time:~6,2%

setlocal

@REM *** START: Turns logging on ***
if "%~1" == "NOWLOGGING" goto NowLogging
set SchedulesLogFile=%temp%\%~n0_%datestamp%.txt
%0 "NOWLOGGING" > "%SchedulesLogFile%" 2>&1
goto :EOF
:NowLogging
@REM *** END: Turns logging on ***


@rem Guts of batch file...
echo [%date% %time%] starting...
@echo on

net stop "ShadowProtect ImageManager"
c:\scripts\sleep.exe 6
net start "ShadowProtect ImageManager"

@echo off
echo [%date% %time%] ending...

Source: http://makemsi-manual.dennisbareis.com/logging_whole_batch_file_execution.htm