SRVSTAT.CMD

:: Version number is 1.9

:: Any queries regarding this script should be sent to 
paul@paulslore.com 

:: This script utilises standard Windows 2000/3 command prompt routines to build a 
:: status snapshot of any Windows NT or 2000/3 server or workstation. It's intention
:: is to provide a daily report in an HTML web page that can be used to quickly 
:: identify problems on the servers. For more detailed information on the script,
:: please read the README.TXT file.

setlocal ENABLEDELAYEDEXPANSION

:: Get parameters from INI file
for /f "eol=; tokens=1,2 delims==" %%a in (srvstat.ini) do set %%a=%%b
for %%a in (!IgnoreEvents!) do echo 	%%a	>>exclid

set tempvar=
if not exist temp\nul md temp
del /q temp\*.*
if exist exclid del exclid

if "!UseLocal!" == "YES" (
	if exist servers.tmp del servers.tmp
	if "!AlternateDomain!" == "" (
		for /f "tokens=1 delims=\ " %%a in ('net view ^|find "\\"') do echo %%a>>servers.tmp
	) else (
		for /f "tokens=1 delims=\ " %%a in ('net view /domain:!AlternateDomain!^|find "\\"') do echo %%a>>servers.tmp
	)
	if not exist servers.tmp (
		echo There was an error enumerating the server names. No servers were found.>srvstat.html
		goto archive
	) else (
		copy /y servers.tmp servers.txt
		del servers.tmp
	)
)

echo Server Name@Registered Owner@Registered Organization@Type@Platform@Version@Service Pack@Build@System Root@Install Date@IP Address@#CPUs@CPU speed@CPU Type@Physical Memory@System Uptime@Reboots 24hrs@7days@Errors 24hrs@7days@Warnings 24hrs@7days>data.csv

for /f %%a in (servers.txt) do (
	psinfo -h -d \\%%a >temp\%%a.srv
	dumpel -l system -s \\%%a -d 1 -m EventLog -e 6009|find /c "0" >temp\%%a.tmp
	for /f %%c in (temp\%%a.tmp) do echo Reboots 24hr: %%c >>temp\%%a.srv
	dumpel -l system -s \\%%a -d 7 -m EventLog -e 6009|find /c "0" >temp\%%a.tmp
	for /f %%c in (temp\%%a.tmp) do echo Reboots 7day: %%c >>temp\%%a.srv
	dumpel -l system -s \\%%a -d 1 -t -format dtTISus >temp\%%a24.evt
	dumpel -l system -s \\%%a -d 7 -t -format dtTISus >temp\%%a7.evt
	find /c "	1	" temp\%%a24.evt >temp\%%a.tmp
	for /f "tokens=1,2 delims=:" %%c in (temp\%%a.tmp) do echo Errors 24hr: %%d >>temp\%%a.srv
	find /c "	1	" temp\%%a7.evt >temp\%%a.tmp
	for /f "tokens=1,2 delims=:" %%c in (temp\%%a.tmp) do echo Errors 7day: %%d >>temp\%%a.srv
	find /c "	2	" temp\%%a24.evt >temp\%%a.tmp
	for /f "tokens=1,2 delims=:" %%c in (temp\%%a.tmp) do echo Warnings 24hr: %%d >>temp\%%a.srv
	find /c "	2	" temp\%%a7.evt >temp\%%a.tmp
	for /f "tokens=1,2 delims=:" %%c in (temp\%%a.tmp) do echo Warnings 7day: %%d >>temp\%%a.srv
	for /f "skip=1 tokens=2 delims=[]" %%x in ('ping -n 1 %%a') do echo IP Address:   %%x >>temp\%%a.srv
)

for %%a in (temp\*.srv) do (
	set tempvar=
	set server=%%a
	set server=!server:~5,-4!

	for %%b in ("Registered owner","Registered organization","Product type","Kernel version","Product version","Service pack","Kernel build number","System root","Install date","IP Address","Processors","Processor speed","Processor type","Physical memory","Uptime") do (
		find /i %%b %%a
		if not errorlevel 1 (
			for /f "tokens=1* delims=:" %%f in (%%a) do if "%%f" == %%b (
				if %%b == "Uptime" (
					for /f "tokens=1,3,5 delims= " %%c in ("%%g") do set tempvar=!tempvar!@%%c Days %%d Hrs %%e Mins
				) else (
					set tempvar=!tempvar!@%%g
				)
			)
		) else (
			set tempvar=!tempvar!@Unknown
		)
	)
	for %%e in ("Reboots 24hr","Reboots 7day","Errors 24hr","Errors 7day","Warnings 24hr","Warnings 7day") do (
		find /i %%e %%a
		if not errorlevel 1 (
			for /f "tokens=1* delims=:" %%b in (%%a) do if "%%b" == %%e set tempvar=!tempvar!@%%c
		) else (
			set tempvar=!tempvar!@Unknown
		)
	)
	echo !server!!tempvar!>>data.csv
)

:: Build the HTML page

echo ^<html^>>srvstat.html
echo ^<title^>!Title!^</title^>>>srvstat.html
echo ^<body STYLE="font: normal normal normal 10px/11px sans-serif;"^>>>srvstat.html
echo ^<table border="0" width="100%%" cellpadding="2" cellspacing="0" STYLE="font: normal normal normal 10px/10px sans-serif;"^>>>srvstat.html
echo 	^<tr^>>>srvstat.html
echo 		^<td width="10"^>>>srvstat.html
echo 		^<td align="left" STYLE="font: normal normal bold 20pt/24pt sans-serif; background: white; color: darkblue;"^>!Title!^</td^>>>srvstat.html
echo 		^<td align="right" valign="top" width="30%%"^>Last updated at %time:~0,-6% on %date%^</td^>>>srvstat.html

echo 	^</tr^>>>srvstat.html
echo ^</table^>>>srvstat.html

:: OS Info

if not "!ShowOSTable!" == "YES" goto servertable
echo ^<table border="0" width="100%%" cellpadding="2" cellspacing="2" STYLE="font: normal normal normal 11px/12px sans-serif;"^>>>srvstat.html
echo 	^<tr STYLE="font: normal normal bold 13pt/14pt sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 		^<td colspan="9" align="center"^>Operating System Information^</td^>>>srvstat.html
count=0
for /f "tokens=1-22 delims=@" %%a in (data.csv) do (
	set /a count=!count! + 1
	echo 	^</tr^>>>srvstat.html
	if !count! == 1 (
		echo 	^<tr STYLE="font-weight: bold; background: #3F7FAF; color: white;"^>>>srvstat.html
	) else (
		echo 	^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
	)
	echo 		^<td^>%%a^</td^>>>srvstat.html
	echo 		^<td^>%%c^</td^>>>srvstat.html
	echo 		^<td^>%%e^</td^>>>srvstat.html
	echo 		^<td^>%%d^</td^>>>srvstat.html
	echo 		^<td align=center^>%%f^</td^>>>srvstat.html
	echo 		^<td align=center^>%%g^</td^>>>srvstat.html
	echo 		^<td align=center^>%%h^</td^>>>srvstat.html
	echo 		^<td^>%%i^</td^>>>srvstat.html
	echo 		^<td^>%%j^</td^>>>srvstat.html
)

echo 	^</tr^>>>srvstat.html
echo ^</table^>^<br^>>>srvstat.html

:: Server Info
:servertable
if not "!ShowServerTable!" == "YES" goto drivetable
echo ^<table border="0" width="100%%" cellpadding="2" cellspacing="2" STYLE="font: normal normal normal 11px/12px sans-serif;"^>>>srvstat.html
echo 	^<tr STYLE="font: normal normal bold 13pt/14pt sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 		^<td colspan="13" align="center"^>Server Information^</td^>>>srvstat.html
set count=0
for /f "tokens=1-22 delims=@" %%a in (data.csv) do (
	echo 	^</tr^>>>srvstat.html
	set /a count=!count! + 1
	if !count! == 1 (
		echo 	^<tr STYLE="font-weight: bold; background: #3F7FAF; color: white;"^>>>srvstat.html
	) else (
		echo 	^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
	)
	echo 		^<td^>%%a^</td^>>>srvstat.html
	echo 		^<td^>%%k^</td^>>>srvstat.html
	echo 		^<td align=center^>%%l^</td^>>>srvstat.html
	echo 		^<td align=center^>%%m^</td^>>>srvstat.html
	echo 		^<td^>%%n^</td^>>>srvstat.html
	echo 		^<td^>%%o^</td^>>>srvstat.html
	echo 		^<td^>%%p^</td^>>>srvstat.html
	if "%%q" == "Reboots 24hrs" (
		echo 		^<td^>%%q^</td^>>>srvstat.html
	) else if %%q GTR !NumReboots1DayAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%q^</td^>>>srvstat.html
	) else if %%q GTR !NumReboots1DayWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%q^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%q^</td^>>>srvstat.html
	)	
	if "%%r" == "7days" (
		echo 		^<td^>%%r^</td^>>>srvstat.html
	) else if %%r GTR !NumReboots1WeekAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%r^</td^>>>srvstat.html
	) else if %%r GTR !NumReboots1WeekWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%r^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%r^</td^>>>srvstat.html
	)	
	if "%%s" == "Errors 24hrs" (
		echo 		^<td^>%%s^</td^>>>srvstat.html
	) else if %%s GTR !NumError1DayAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%s^</td^>>>srvstat.html
	) else if %%s GTR !NumError1DayWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%s^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%s^</td^>>>srvstat.html
	)	
	if "%%t" == "7days" (
		echo 		^<td^>%%t^</td^>>>srvstat.html
	) else if %%t GTR !NumError1WeekAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%t^</td^>>>srvstat.html
	) else if %%t GTR !NumError1WeekWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%t^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%t^</td^>>>srvstat.html
	)	
	if "%%u" == "Warnings 24hrs" (
		echo 		^<td^>%%u^</td^>>>srvstat.html
	) else if %%u GTR !NumWarnings1DayAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%u^</td^>>>srvstat.html
	) else if %%u GTR !NumWarnings1DayWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%u^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%u^</td^>>>srvstat.html
	)	
	if "%%v" == "7days" (
		echo 		^<td^>%%v^</td^>>>srvstat.html
	) else if %%v GTR !NumWarnings1WeekAlert! (
		echo 		^<td align="center" style="font-weight: bold; background: red; color: white"^>%%v^</td^>>>srvstat.html
	) else if %%v GTR !NumWarnings1WeekWarn! (
		echo 		^<td align="center" style="font-weight: bold; background: orange; color: black"^>%%v^</td^>>>srvstat.html
	) else (
		echo 		^<td align="center"^>%%v^</td^>>>srvstat.html
	)	
)
echo 	^</tr^>>>srvstat.html
echo ^</table^>^<br^>>>srvstat.html

:drivetable
echo ^<table width="100%%" border="0" cellpadding="0" cellspacing="0"^>>>srvstat.html
echo 	^<tr valign="top"^>>>srvstat.html
echo 		^<td^>>>srvstat.html

:: Drive Status
if not "!ShowDriveTable!" == "YES" goto hotfixtable
echo 			^<table border="0" width="260" cellpadding="2" cellspacing="2" STYLE="font: normal normal normal 11px/12px sans-serif;"^>>>srvstat.html
echo 				^<tr STYLE="font: normal normal bold 13pt/14pt sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 					^<td colspan="5"^>^<center^>Drive Status^</center^>^</td^>>>srvstat.html
echo 				^</tr^>>>srvstat.html
echo 				^<tr STYLE="font: normal normal bold 11px/12px sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 					^<td^>Drive^</td^>>>srvstat.html
echo 					^<td^>FS Type^</td^>>>srvstat.html
echo 					^<td^>MB Total^</td^>>>srvstat.html
echo 					^<td^>MB Free^</td^>>>srvstat.html
echo 					^<td^>%% Free^</td^>>>srvstat.html
echo 				^</tr^>>>srvstat.html

for /f "tokens=1 delims= " %%a in (servers.txt) do (
	echo 				^<tr style="background: #3F7FAF; color: white;"^>>>srvstat.html
	echo 					^<td colspan="5" align="center"^>^<b^>%%a^</b^>^</td^>>>srvstat.html
 	echo 				^</tr^>>>srvstat.html
	for /f "usebackq tokens=1-9 delims= " %%b in (`findstr /i /c:": Fixed" temp\%%a.srv`) do (
		echo 				^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
		if "%%j" == "" (
			set drsize=%%e %%f
			set drfree=%%g %%h
			set drpcfree=%%i
		) else (
			set drsize=%%f %%g
			set drfree=%%h %%i
			set drpcfree=%%j
		)
		echo 					^<td^>%%b^</td^>>>srvstat.html
		echo 					^<td^>%%d^</td^>>>srvstat.html
		echo 					^<td^>!drsize!^</td^>>>srvstat.html
		echo 					^<td^>!drfree!^</td^>>>srvstat.html
		set calc=!drfree:~0,-5!
		set calc=!drpcfree:~0,-1!
		if !calc! LSS %DriveSpacePercentAlert% (
			echo 					^<td align="center" style="font-weight: bold; background: red; color: white"^>!drpcfree!^</td^>>>srvstat.html
		) else if !calc! LSS %DriveSpacePercentWarn% (
			echo 					^<td align="center" style="font-weight: bold; background: orange; color: black"^>!drpcfree!^</td^>>>srvstat.html
		) else (
			echo 					^<td align="center"^>!drpcfree!^</td^>>>srvstat.html
		)
		echo 				^</tr^>>>srvstat.html	
	)
)
echo 			^</table^>>>srvstat.html
echo 		^</td^>>>srvstat.html
echo 		^<td width="10"^>^ ^</td^>>>srvstat.html
echo 		^<td^>>>srvstat.html

:: Hotfixes
:hotfixtable
if not "!ShowHotfixTable!" == "YES" goto errortable
echo 			^<table border="0" width="80" cellpadding="2" cellspacing="2"STYLE="font: normal normal normal 11px/12px sans-serif;"^>>>srvstat.html
echo 				^<tr STYLE="font: normal normal bold 13pt/14pt sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 					^<td^>^<center^>Hotfix^</center^>^</td^>>>srvstat.html
echo 					^<td^>^<center^>Installed^</center^>^</td^>>>srvstat.html
echo 				^</tr^>>>srvstat.html	
for /f %%a in (servers.txt) do (
	echo 				^<tr style="background: #3F7FAF; color: white;"^>>>srvstat.html	
	echo 					^<td colspan=2 align=center^>^<b^>%%a^</b^>^</td^>>>srvstat.html
	echo 				^</tr^>>>srvstat.html
	for /f "usebackq skip=1 tokens=1* delims= " %%b in (`findstr /c:"/" temp\%%a.srv`) do (
		echo 				^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
		echo 					^<td^>%%b^</td^>>>srvstat.html
		echo 					^<td^>%%c^</td^>>>srvstat.html
		echo 				^</tr^>>>srvstat.html
	)
)
echo 			^</table^>>>srvstat.html
echo 		^</td^>>>srvstat.html
echo 		^<td width="10"^>^ ^</td^>>>srvstat.html
echo 		^<td^>>>srvstat.html

:: Errors and Warnings
:errortable
if not "%ShowErrorTable%" == "YES" goto endtable

echo 			^<table border="0" width="100%%" cellpadding="2" cellspacing="2" STYLE="font: normal normal normal 11px/12px sans-serif;"^>>>srvstat.html
echo 				^<tr STYLE="font: normal normal bold 13pt/14pt sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html

echo 					^<td colspan=6^>^<center^>Errors and Warnings ( Last 24 Hours)^</center^>^</td^>>>srvstat.html
echo 				^</tr^>>>srvstat.html
echo 				^<tr STYLE="font: normal normal bold 11px/12px sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo 					^<td^>Date^</td^>>>srvstat.html
echo 					^<td^>Time^</td^>>>srvstat.html
echo 					^<td^>ID^</td^>>>srvstat.html
echo 					^<td^>Source^</td^>>>srvstat.html
echo 					^<td^>User^</td^>>>srvstat.html
echo 					^<td^>Detail^</td^>>>srvstat.html
echo 				^</tr^>>>srvstat.html

for /f %%a in (servers.txt) do (
echo					^<tr STYLE="font: normal normal bold 11px/12px sans-serif; background: #3F7FAF; color: white;"^>>>srvstat.html
echo						^<td colspan="6" align="center"^>%%a^</td^>>>srvstat.html
echo					^</tr^>>>srvstat.html
	for /f "tokens=1-7 delims=	" %%b in (temp\%%a24.evt) do (
		find "	%%e	" exclid
		if errorlevel 1 (
			if "%%d" == "2" (
				echo					^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
				echo						^<td^>%%b^</td^>>>srvstat.html
				echo						^<td^>%%c^</td^>>>srvstat.html
				echo						^<td align="center" style="font-weight: bold; background: orange; color: black;"^>%%e^ ^<a href="http://www.eventid.net/display.asp?eventid=%%e" target="_blank"^>^<img src="idinfo.gif" border="0" alt="Click for more information"^>^</a^>^</td^>>>srvstat.html
				echo						^<td^>%%f^</td^>>>srvstat.html
				echo						^<td^>%%g^</td^>>>srvstat.html
				echo						^<td^>%%h^</td^>>>srvstat.html
				echo					^</tr^>>>srvstat.html
			)
		)
	)
	for /f "tokens=1-7 delims=	" %%b in (temp\%%a24.evt) do (	
		find "	%%e	" exclid
		if errorlevel 1 (
			if "%%d" == "1" (
				echo					^<tr STYLE="background: #F0EFE0;"^>>>srvstat.html
				echo						^<td^>%%b^</td^>>>srvstat.html
				echo						^<td^>%%c^</td^>>>srvstat.html
				echo						^<td align="center" style="font-weight: bold; background: red; color: white;"^>%%e^ ^<a href="http://www.eventid.net/display.asp?eventid=%%e" target="_blank"^>^<img src="idinfo.gif" border="0" alt="Click for more information"^>^</a^>^</td^>>>srvstat.html
				echo						^<td^>%%f^</td^>>>srvstat.html
				echo						^<td^>%%g^</td^>>>srvstat.html
				echo						^<td^>%%h^</td^>>>srvstat.html
				echo					^</tr^>>>srvstat.html
			)		
		)
	)
)
echo 			^</table^>>>srvstat.html
:endtable

echo 		^</td^>>>srvstat.html
echo 	^</tr^>>>srvstat.html
echo ^</table^>>>srvstat.html
echo ^<br^>>>srvstat.html
echo ^<font size="2"^>Archive reports can be found on the ^<a href="archive.html"^>ARCHIVE^</a^> page.>>srvstat.html
echo ^<br^>>>srvstat.html
echo The raw data used to generate this page can be downloaded ^<a href="data.txt"^>HERE^</a^>. (Tab seperated values, right click the link-choose save as)^</font^>>>srvstat.html
echo ^</body^>>>srvstat.html
echo ^</html^>>>srvstat.html
if exist data.txt del data.txt
for /f "tokens=1-25 delims=@" %%a in (data.csv) do echo %%a	%%b	%%c	%%d	%%e	%%f	%%g	%%h	%%i	%%j	%%k	%%l	%%m	%%n	%%o	%%p	%%q	%%r	%%s	%%t	%%u	%%v	%%w	%%x	%%y>>data.txt

:archive
if not exist archive\nul md archive
for /f "tokens=2,3,4 delims=/ " %%a in ("%date%") do set today=%%c%%b%%a
for /f "tokens=3,4 delims=/ " %%a in ("%date%") do set month=%%b-%%a
if not exist archive\!month!\nul md archive\!month!
if not exist archive\!month!\idinfo.gif copy idinfo.gif archive\!month!
copy srvstat.html archive\!month!\stat-!today!.html

:: Build Archive Page
echo ^<html^>>archive.html
echo ^<head^>>>archive.html
echo ^<title^>Server Stats Archive Files^</title^>>>archive.html
echo ^</head^>>>archive.html
echo ^<body STYLE="font: normal normal normal 12pt/14pt sans-serif;"^>>>archive.html
echo ^<center^>^<h2^>^<font color="darkblue"^>Server Status Archive Files^</font^>^</h2^>^<br^>>>archive.html
echo ^<table border="0" cellpadding="4" cellspacing="2" STYLE="font: normal normal normal 11px/12px sans-serif; background: white;"^>>>archive.html

for /F %%a in ('dir /b /o-n archive') do (
	echo 	^<tr^>>>archive.html
	echo 		^<td STYLE="font: bold; background: #3F7FAF; color: white;"^>%%a^</td^>>>archive.html
	for /f %%b in ('dir /b archive\%%a\*.html') do (
		set file=%%b
		echo 		^<td STYLE="background: #F0EFE0;"^>^<a href="archive/%%a/%%b"^>!file:~11,-5!^</a^>^</td^>>>archive.html
	)
	echo 	^<tr^>>>archive.html
)

echo ^</table^>>>archive.html
echo ^</center^>>>archive.html
echo ^</body^>>>archive.html
echo ^</html^>>>archive.html

:: Copy pages to web folder
:webcopy
if not "%CopyToWeb%" == "YES" goto smtpsend
if "!MapWebFolder!" == "YES" net use !MapDrive! /d && net use !MapDrive! !MapShare!
if not exist "!wwwdir!nul" md "!wwwdir!"
if not exist "!wwwdir!nul" goto smtpsend
copy /y srvstat.html "!wwwdir!"
copy /y archive.html "!wwwdir!"
if not exist !wwwdir!archive\nul md !wwwdir!archive
if not exist !wwwdir!archive\!month!\nul md !wwwdir!archive\!month!
if not exist !wwwdir!archive\!month!\idinfo.gif copy idinfo.gif !wwwdir!archive\!month!
if not exist !wwwdir!archive\!month!\stat-!today!.html copy /y srvstat.html !wwwdir!archive\!month!\stat-!today!.html
copy /y data.txt !wwwdir!
copy /y *.gif !wwwdir!

:smtpsend
if not "%SendSMTP%" == "YES" goto end
smtpsend -f!SMTPfrom! -t!SMTPto! -h!SMTPhost! -s!SMTPsubject! -aarchive\!month!\stat-!today!.html

:end
endlocal