DASHBOARD.CMD

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

:: More scripts can be found at http://www.paulslore.com

@echo off
setlocal ENABLEDELAYEDEXPANSION

:: The only argument is the working directory, necessary when running as a service.
:: If no working directory is specified it is automatically detected.
if not "%1" == "" (
	cd /d "%1"
) else (
	set curdir=%cd%
	cd /d %~dp0
)

>error.log echo Error Log for Dashboard Script
>>error.log echo ------------------------------
if not exist openssl.exe >>error.log echo !date! !time:~0,-3! - OPENSSL.EXE not found. No password hash will be available.

:: Get parameters from INI file
echo Reading configuration parameters
if not exist dashboard.ini >>error.log echo !date! !time:~0,-3! - No INI file found, using defaults.
for /f "eol=; tokens=1* delims==" %%a in (dashboard.ini) do set %%a=%%b
if "!PageTitle!" == "" set PageTitle=Server Status Dashboard
if "!RunEvery!" == "" set RunEvery=2
if "!ReportFile!" == "" set ReportFile=default.htm
if not "!WMIUser!" == "" set WMIUser=%username%
for /f "tokens=* delims= " %%a in ('dsquery * -filter "name=%computername%" -attr objectGUID') do set guid=%%a
if not "!PWHash!" == "" (
	for /f "tokens=*" %%a in ('echo !PWHash!^|sed15 "s/=/^^=/g"') do set PWHash=%%a
)
if not "!WebPWHash!" == "" (
	for /f "tokens=*" %%a in ('echo !WebPWHash!^|sed15 "s/=/^^=/g"') do set WebPWHash=%%a
)
if not "!Filter!" == "" (
	if "!Filter:~0,1!" == "-" (
		set Filter=^|find /v /i "!Filter:~1!"
	) else set Filter=^|find /i "!Filter!"
)
if "!DomainList!" == "" (
	for /f "tokens=1-5 delims=." %%a in ("%userdnsdomain%") do (
		if not "%%a" == "" set DomainList=DC=%%a
		if not "%%b" == "" set DomainList=!DomainList!,DC=%%b
		if not "%%c" == "" set DomainList=!DomainList!,DC=%%c
		if not "%%d" == "" set DomainList=!DomainList!,DC=%%d
		if not "%%e" == "" set DomainList=!DomainList!,DC=%%e
	)
	if not "!DomainList!" == "" set DomainList="!DomainList!"
)
if "!DomainList!" == "" >>error.log echo !date! !time:~0,-3! - No Domain found.&goto error

>>error.log echo.
>>error.log echo Configuration parameters used:
>>error.log echo ----------------------------------------------------------------------
>>error.log echo PageTitle - !PageTitle!
>>error.log echo RunEvery - !RunEvery!
>>error.log echo DomainList - !DomainList!
>>error.log echo CheckVM - !CheckVM!
>>error.log echo UsePWHash - !UsePWHash!
>>error.log echo ReportFile - !ReportFile!
>>error.log echo Filter - !Filter!
>>error.log echo CopyToWeb - !CopyToWeb!
>>error.log echo WebUNC - !WebUNC!
>>error.log echo WebUser - !WebUser!
>>error.log echo ----------------------------------------------------------------------
>>error.log echo End of configuration parameters.
>>error.log echo.

:beginning
call :gettime now

set status=good

echo Getting list of servers
>servers.txt echo ;List of servers from AD

for %%a in (!DomainList!) do for /f "delims= " %%b in ('dsquery * %%a -limit 100000 -filter "(&(objectClass=computer)(operatingSystem=*server*))" -attr name!Filter!^|find /v "name"^|sort') do call :getserver %%b %%a

echo Creating report
(
echo ^<html^>
echo ^<head^>
echo 	^<title^>!PageTitle!^</title^>
echo ^<meta http-equiv="refresh" content="120"^> 
echo ^<style^>
echo table {
echo 	background: white;
echo 	border-color: darkblue;
echo 	width: 100%%;
echo 	border-collapse: collapse;
echo 	font: normal normal normal 10px/10px sans-serif;
echo }
echo th {
echo 	background: darkblue;
echo 	color: white;
echo 	font-size: 11px;
echo 	font-weight: bold;
echo 	border-color: darkblue;
echo 	padding-left: 5px;
echo 	padding-right: 5px;
echo }
echo td {
echo 	border-color: darkblue;
echo 	padding-left: 5px;
echo 	padding-right: 5px;
echo }
echo .head {
echo 	background: transparent;
echo 	border-color: darkblue;
echo 	width: 100%%;
echo 	border-collapse: collapse;
echo }
echo .head td {
echo 	padding: 5px;
echo }
echo input {
echo 	font-size: 10px;
echo }
echo ^</style^>
echo ^<script type="text/javascript" language="JavaScript"^>
echo.
echo function DoSearch^(d^) {
echo 	var rows = document.getElementsByTagName^('tr'^);
echo 	var found = 0;
echo.	
echo 	for ^(var i = 2; i ^< rows.length; i++^) {
echo 		rows[i].style.display = "";
echo 		found++;
echo 		if^(^^!d.length ^< 1^) { 
echo 			var row = rows[i];
echo 			if^(^^!row.innerHTML.toUpperCase^(^).match^(d.toUpperCase^(^)^)^) {
echo 				rows[i].style.display = "none";
echo 				found--;
echo 			}
echo 		}
echo 	}
echo 	document.search.found.value = found;
echo }
echo function ClearSearch^(^) {
echo 	var rows = document.getElementsByTagName^('tr'^);
echo 	var found = 0;
echo.
echo 	for ^(var i = 2; i ^< rows.length; i++^) {
echo 		rows[i].style.display = "";
echo 		found++;
echo 	}
echo 	document.search.string.value = "";
echo 	document.search.found.value = found;
echo }
echo ^</script^>
echo ^</head^>
echo ^<body onLoad="ClearSearch();"^>
echo ^<table border="0" class="head" height="30"^>
echo ^<tr^>
echo	^<td align="left"^>^<font size="4"^>!PageTitle!^</font^>^</td^>
echo	^<form name="search" onSubmit="return false;"^>
echo	^<td^>
echo			^<input name="string" type="text" size="8" onkeyDown="if(event.keyCode==13){DoSearch(document.search.string.value);}"^>
echo			^<input type="button" value="Filter" name="Find" onclick="DoSearch(document.search.string.value);"^>
echo			^<input type="button" value="Show All" onclick="ClearSearch();"^>
echo	^</td^>
echo	^<td style="font-size: 12px;"^>
echo			^<input type="text" name="found" size="1" style="background:transparent; border: 0px;"^>Servers Found
echo	^</td^>
echo	^</form^>
echo	^<td align="right"^>Last Updated on !date! at !time:~0,-3!^</td^>
echo ^</tr^>
echo ^</table^>

echo ^<table border="1"^>
echo ^<tr^>
echo 	^<th^>Host^</th^>
echo 	^<th^>DNS Domain^</th^>
echo 	^<th align="center"^>Operating System^</th^>
echo 	^<th align="center"^>Service Pack^</th^>
echo 	^<th align="center"^>Version^</th^>
echo 	^<th align="center"^>IP Address^</th^>
if /i "!CheckVM!" == "YES" echo 	^<th align="center"^>VM^</th^>
echo 	^<th width="50" align="center"^>RDP Link^</th^>
echo 	^<th width="40"^>Status^</th^>
echo ^</tr^>
)>dashboard.html

for /f "tokens=1-5 delims=	" %%a in (servers.txt) do (
	echo Querying server %%a
	set ip=
	set alive=no
	set exclude=no
	set up=no
	set isvm=
	if exist exclusions.txt >nul find /i "%%a" exclusions.txt&&set exclude=yes
	for /f "tokens=1* delims=." %%f in ("%%e") do set domain=%%g
	for /f "skip=1 tokens=2-5 delims=:. " %%f in ('nslookup %%e !domain! 2^>nul^|find "Address:"') do set ip=%%f.%%g.%%h.%%i
	(
	echo ^<tr id="%%a" height="18" onMouseover="this.style.backgroundColor='yellow';" onMouseout="this.style.backgroundColor='';"^>
	echo 	^<td^>%%a^</td^>
	echo 	^<td^>!domain!^</td^>
	echo 	^<td align="center"^>%%b^</td^>
	echo 	^<td align="center"^>%%c^</td^>
	echo 	^<td align="center"^>%%d^</td^>
	echo 	^<td align="center"^>!ip!^</td^>
	)>>dashboard.html
	ping -w 500 -n 1 !ip!|>nul find /i "reply"&&set up=yes
	if /i "!CheckVM!" == "YES" (
		if not "!ip!" == "" (
			if /i "!up!" == "yes" (
				if /i not "!PWHash!" == "" >nul (for /f %%m in ('echo !PWHash!^|2^>^>error.log openssl aes-256-ecb -a -d -k %computername%%username%!guid!') do set myp=%%m)
				if /i "!WMIAuth!" == "yes" (
					if /i "%%a" == "%computername%" (
						for /f "skip=1 tokens=1" %%m in ('2^>^>error.log wmic /node:!ip! csproduct get name^|find "e"') do if not "%%m" == "" set isvm=%%m
					) else for /f "skip=1 tokens=1" %%m in ('2^>^>error.log wmic /user:!domain!\!WMIUser! /password:!myp! /node:!ip! csproduct get name^|find "e"') do if not "%%m" == "" set isvm=%%m
				) else for /f "skip=1 tokens=1" %%m in ('2^>^>error.log wmic /node:!ip! csproduct get name^|find "e"') do if not "%%m" == "" set isvm=%%m
				if /i "!isvm!" == "VMWare" (
					>>dashboard.html echo 	^<td align="center"^>^<img src="vm.gif" border="0"^>^</td^>
				) else >>dashboard.html echo 	^<td align="center"^>^</td^>
			) else >>dashboard.html echo 	^<td align="center"^>?^</td^>
		) else >>dashboard.html echo 	^<td align="center"^>?^</td^>
	)
	(
	if not "!ip!" == "" (
		if /i "!up!" == "yes" (
			portqry -n !ip! -e 3389 -i >nul
			if errorlevel 1 (
				echo 	^<td align="center" style="background: orange;"^>^<a href="rdp.aspx?host=%%a&ip=!ip!&domain=!domain!"^>^<img src="rdp.gif" border="0" height="14"^>^</a^>^</td^>
			) else 			echo 	^<td align="center" style="background: lightgreen;"^>^<a href="rdp.aspx?host=%%a&ip=!ip!&domain=!domain!"^>^<img src="rdp.gif" border="0" height="14"^>^</a^>^</td^>
		) else 	echo 	^<td align="center"^>^</td^>
	) else 	echo 	^<td align="center"^>^</td^>
	if /i "!up!" == "no" (
		if /i not "!exclude!" == "yes" (
			set status=bad
			echo 	^<td align="center" style="background: red; color: white;"^>^<b^>Offline^</b^>^</td^>
		) else echo 	^<td align="center"^>^<b^>Offline^</b^>^</td^>
	) else (
		echo 	^<td style="background: green;"^>^ ^</td^>
	)
	echo ^</tr^>
	)>>dashboard.html
)

(
echo ^</table^>
echo ^<style^>
echo body {
echo 	font: normal normal normal 11px/12px sans-serif;
echo 	color: darkblue;
if /i "!status!" == "good" (
	echo 	background: lightgreen;
) else echo 	background: orange;
echo }
echo ^</style^>
)>>dashboard.html

call :gettime then

if !then! LSS !now! set /a then=!then!+24000000
set /a timediff=!then!-!now!
set /a sleeptime=!now!+(!RunEvery!*6000)
set /a sleeptime=!sleeptime!-!then!
for %%a in (hrs mins secs) do set %%a=0
set /a mins=!timediff:~0,-2!/60
if !mins! GTR 59 set /a hrs=!mins!/60
set /a secs=!timediff:~0,-2!-(!mins!*60)
set /a mins=!mins!-(!hrs!*60)
echo Script took !hrs! hours !mins! minutes and !secs! seconds to complete
(
echo ^</html^>
echo Script took !hrs! hours !mins! minutes and !secs! seconds to complete
)>>dashboard.html
>nul copy /y dashboard.html "!ReportFile!"

echo Finished report

if /i "!CopyToWeb!" == "YES" (
	if /i not "!WebUser!" == "" (
		if /i not "!WebPWHash!" == "" >nul (for /f %%a in ('echo !WebPWHash!^|2^>^>error.log openssl aes-256-ecb -a -d -k %computername%%username%!guid!') do set wpw=%%a)
		for /f "tokens=1,2 delims=\" %%a in ("!WebUNC!") do >nul 2>>error.log net use \\%%a\%%b /user:!WebUser! !wpw!
	)
	echo Copying report to web server
	>nul copy /y "!ReportFile!" "!WebUNC!"
	>nul copy /y "*.asp*" "!WebUNC!"
	>nul copy /y "*.gif" "!WebUNC!"
	>nul copy /y "*.config" "!WebUNC!"
)
if !sleeptime! GTR 0 (
	echo Waiting for !sleeptime!0 milliseconds
	sleep -m !sleeptime!0
)
goto beginning

:getserver
set data=%1

for /f "tokens=1* delims=: " %%b in ('dsquery * %2 -filter "(&(objectClass=computer)(name=%1))" -attr operatingSystem operatingSystemServicePack operatingSystemVersion dNSHostName /l') do if "%%c" == "" (set data=!data!	-) else set data=!data!	%%c
>>servers.txt echo !data!
goto:eof

:gettime
for /f "tokens=1-4 delims=:." %%a in ("%time%") do (
	set tmpvar=%%a
	if !tmpvar:~0^,1! == 0 (
		set /a %1=!tmpvar:~1!*3600
	) else set /a %1=!tmpvar!*3600
	set tmpvar=%%b
	if !tmpvar:~0^,1! == 0 (
		set /a %1=!%1!+^(!tmpvar:~1!*60^)
	) else set /a %1=!%1!+^(!tmpvar!*60^)
	set tmpvar=%%c
	if !tmpvar:~0^,1! == 0 (
		set /a %1=!%1!+!tmpvar:~1!
	) else set /a %1=!%1!+!tmpvar!
	set /a %1=!%1!*100
	set tmpvar=%%d
	if !tmpvar:~0^,1! == 0 (
		set /a %1=!%1!+!tmpvar:~1!
	) else set /a %1=!%1!+!tmpvar!
)
goto:eof

cd !curdir!
:error
echo There was an error running the script! Please check the ERROR.LOG file for details.
exit /b 1