Link to home
Start Free TrialLog in
Avatar of getazhar
getazharFlag for India

asked on

Dailty Uptime Report in HTML format

Hi Experts,

am looking for a Script to monitor the server and send Daily Uptime Report in HTML format.
would appreciate if color code can be used as in this link: http://virtu-al.net/Downloads/vCheck/vCenter01vCheck_26-3-2010.htm

Downtime less than 5 days should be highlighted in red an other should be in green.

Thanks in Advance,

Regards,
Ameer
Avatar of prashanthd
prashanthd
Flag of India image

Do you want  single report for multiple servers?
Avatar of getazhar

ASKER

Yes...
Try the following powershell script.

Modify the following parameters....

$server_list = "C:\list.txt"
$html_path= "C:\Uptime_Report.html"
$smtp_Server=""
$mailfrom=""
$mailto=""
$server_list = "C:\list.txt"
$html_path= "C:\Uptime_Report.html"
$smtp_Server=""
$mailfrom=""
$mailto=""

$strhtml1=""
$strhtml3=""
$strhtml1 = $strhtml1 + ("<html>")
$strhtml1 = $strhtml1 + ("<head>")
$strhtml1 = $strhtml1 + ("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>")
$strhtml1 = $strhtml1 + ("<style>")
$strhtml1 = $strhtml1 + ("<!--")
$strhtml1 = $strhtml1 + ("body, td{font-family:tahoma;font-size:11px;border: border-width:1px; border-style:solid ; color #CCCCCC;}")
$strhtml1 = $strhtml1 + ("<style>")
$strhtml1 = $strhtml1 + ("<!--")
$strhtml1 = $strhtml1 + ("body, td {font-family:tahoma;font-size:11px;border: border-width:1px; border-style:solid ; color #CCCCCC;}")
$strhtml1 = $strhtml1 + ("table{border: thin solid #000000;}")
$strhtml1 = $strhtml1 + ("-->")
$strhtml1 = $strhtml1 + ("</style>")

$strhtml1 = $strhtml1 + ("<table border='1' width='100%' align='center' cellpadding='3' cellspacing='0' style='border-top: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;'>")
$strhtml1 = $strhtml1 + ("<tr>")
$strhtml1 = $strhtml1 + ("<td colspan='4' width='100%' align='center' bgcolor='#999999'><strong><font size='+1'>Uptime Report</font></strong></td>")
$strhtml1 = $strhtml1 + ("</tr>")
$strhtml1 = $strhtml1 + ("<tr>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>Server</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>ServerLocalTime</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>LastBootUpTime</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>Uptime Days</strong></div></td>")
$strhtml1 = $strhtml1 + ("</tr>")

$serverlist = Get-Content $server_list
foreach ($server in $serverlist){
$servername=""
$lctime=""
$lastboot=""
$daydiff=""

$servername = $server
$ctr = $ctr + 1
$ctr
$servername
#Get Server Local Time
$ltime = Get-WmiObject Win32_LocalTime -ComputerName $servername
$lctime = $ltime.Month.ToString() + "/" + $ltime.Day.ToString() + "/" +$ltime.Year.ToString() + " " +$ltime.Hour.ToString() + ":" +$ltime.Minute.ToString() 
$lctime

#Get Server Last Boot Time
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $servername
$servername
$lastboot = $os.lastbootuptime 
$lastboot = [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboot) 
$lastboot

#Get Server Uptime in days
$daydiff = New-TimeSpan $(Get-Date -month $lastboot.month -day $lastboot.day -year $lastboot.year -hour $lastboot.hour -minute $lastboot.minute) $(Get-Date -month $ltime.month -day $ltime.day -year $ltime.year -hour $ltime.hour -minute $ltime.minute)
$daydiff = $daydiff.days
$daydiff

$strhtml3 = $strhtml3 + ("<tr>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $servername + "</div></td>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lctime + "</div></td>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lastboot + "</div></td>")

if($daydiff -gt 5){
			$strhtml3 = $strhtml3 + ("<td><div align='center' bgcolor='#BBFBA8'>"+ $daydiff + "</div></td>")}
		else{
			$strhtml3 = $strhtml3 + ("<td align='center' bgcolor='#ff0000' ><strong>"+ $daydiff + "</strong></td>")
		}
$strhtml3 = $strhtml3 + ("</tr>")

}

$strhtml1 + $strhtml3 | Out-File $html_path

$FromAddress = $mailfrom

$ToAddress = $mailto

$MessageSubject = "Servers Uptime Report"

$MessageBody = "Attached is Report"

$SmtpServer = $smtp_Server

$Attachment = $html_path

Send-MailMessage -From $FromAddress -To $ToAddress -Subject $MessageSubject -Body $MessageBody -SmtpServer $SmtpServer -Attachments $Attachment

Open in new window

Impressive but small tweaks required..

if you change the fonts & colors would be good. also in place of heading, can I add a jpg file, I mean customer logo ?

when the server's Uptime is low.. the entire values in the row should get highlighted in BOLD letters (i.e.Server ServerLocalTime LastBootUpTime Uptime Days) red color is not required I believe.

UptimeDays coloum is giving uptime in numbers only... can we add "Days" to the numbers showing there ?? that will look good I think.

one last point is -BodyAsHtml... Instead of attachment, could you please use -BodyAsHtml option in this code ?

Thanks in Advance,

Regards,
Ameer


if you change the fonts & colors would be good. also in place of heading, can I add a jpg file, I mean customer logo ?

Fonts and Colors are something I am not good at...A jpg can be added if it can be mapped to  a source, but cannot be embedded. We can for example give a url of image source, if it is available on some website, but again it requires formatting.

when the server's Uptime is low.. the entire values in the row should get highlighted in BOLD letters (i.e.Server ServerLocalTime LastBootUpTime Uptime Days) red color is not required I believe.

Done..

UptimeDays coloum is giving uptime in numbers only... can we add "Days" to the numbers showing there ?? that will look good I think.

Done..

one last point is -BodyAsHtml... Instead of attachment, could you please use -BodyAsHtml option in this code ?

Done..


$server_list = "C:\list.txt"
$html_path= "C:\Uptime_Report.html"
$smtp_Server=""
$mailfrom=""
$mailto=""

$strhtml1=""
$strhtml3=""
$strhtml1 = $strhtml1 + ("<html>")
$strhtml1 = $strhtml1 + ("<head>")
$strhtml1 = $strhtml1 + ("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>")
$strhtml1 = $strhtml1 + ("<style>")
$strhtml1 = $strhtml1 + ("<!--")
$strhtml1 = $strhtml1 + ("body, td{font-family:tahoma;font-size:11px;border: border-width:1px; border-style:solid ; color #CCCCCC;}")
$strhtml1 = $strhtml1 + ("<style>")
$strhtml1 = $strhtml1 + ("<!--")
$strhtml1 = $strhtml1 + ("body, td {font-family:tahoma;font-size:11px;border: border-width:1px; border-style:solid ; color #CCCCCC;}")
$strhtml1 = $strhtml1 + ("table{border: thin solid #000000;}")
$strhtml1 = $strhtml1 + ("-->")
$strhtml1 = $strhtml1 + ("</style>")

$strhtml1 = $strhtml1 + ("<table border='1' width='100%' align='center' cellpadding='3' cellspacing='0' style='border-top: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;border-bottom: 1px solid #CCCCCC;'>")
$strhtml1 = $strhtml1 + ("<tr>")
$strhtml1 = $strhtml1 + ("<td colspan='4' width='100%' align='center' bgcolor='#999999'><strong><font size='+1'>Uptime Report</font></strong></td>")
$strhtml1 = $strhtml1 + ("</tr>")
$strhtml1 = $strhtml1 + ("<tr>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>Server</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>ServerLocalTime</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>LastBootUpTime</strong></div></td>")
$strhtml1 = $strhtml1 + ("<td bgcolor='#CCCCCC' align='left' valign='top'  style='border-bottom: 1px solid #CCCCCC;'><div align='center'><strong>Uptime Days</strong></div></td>")
$strhtml1 = $strhtml1 + ("</tr>")

$serverlist = Get-Content $server_list
foreach ($server in $serverlist){
$servername=""
$lctime=""
$lastboot=""
$daydiff=""

$servername = $server
$ctr = $ctr + 1
$ctr
$servername
#Get Server Local Time
$ltime = Get-WmiObject Win32_LocalTime -ComputerName $servername
$lctime = $ltime.Month.ToString() + "/" + $ltime.Day.ToString() + "/" +$ltime.Year.ToString() + " " +$ltime.Hour.ToString() + ":" +$ltime.Minute.ToString() 
$lctime

#Get Server Last Boot Time
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $servername
$servername
$lastboot = $os.lastbootuptime 
$lastboot = [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboot) 
$lastboot

#Get Server Uptime in days
$daydiff = New-TimeSpan $(Get-Date -month $lastboot.month -day $lastboot.day -year $lastboot.year -hour $lastboot.hour -minute $lastboot.minute) $(Get-Date -month $ltime.month -day $ltime.day -year $ltime.year -hour $ltime.hour -minute $ltime.minute)
$daydiff = $daydiff.days
$daydiff

$strhtml3 = $strhtml3 + ("<tr>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $servername + "</div></td>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lctime + "</div></td>")
$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lastboot + "</div></td>")

if($daydiff -gt 5){
			$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $servername + "</div></td>")
			$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lctime + "</div></td>")
			$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $lastboot + "</div></td>")
			$strhtml3 = $strhtml3 + ("<td><div align='center'>"+ $daydiff + " Days</div></td>")}
		else{
			$strhtml3 = $strhtml3 + ("<td align='center'><strong>"+ $servername + "</strong></td>")
			$strhtml3 = $strhtml3 + ("<td align='center'><strong>"+ $lctime + "</strong></td>")
			$strhtml3 = $strhtml3 + ("<td align='center'><strong>"+ $lastboot + "</strong></td>")
			$strhtml3 = $strhtml3 + ("<td align='center'><strong>"+ $daydiff + " Days</strong></td>")
		}
$strhtml3 = $strhtml3 + ("</tr>")

}

#$strhtml1 + $strhtml3 | Out-File $html_path
$strhtml=""
$strhtml=$strhtml1 + $strhtml3

$FromAddress = $mailfrom

$ToAddress = $mailto

$MessageSubject = "Servers Uptime Report"

#$MessageBody = "Attached is Report"

$SmtpServer = $smtp_Server

#$Attachment = $html_path

Send-MailMessage -From $FromAddress -To $ToAddress -Subject $MessageSubject -BodyAsHtml $strhtml -SmtpServer $SmtpServer

Open in new window

here is the output...something is wrong..
EE-Uptime.png
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Script works perfectly fine.. Appreciate the efforts.