top of page

PowerShell - Server Uptime Report to HTML/Email

  • Writer: Jon Boyette
    Jon Boyette
  • Jan 10, 2022
  • 2 min read

Save as a .ps1, populate c:\temp\servers.txt to check uptime, reports out to C:\temp\ServerUptimeReport.html and smtp settings to configure in script

$ServerNameOption = ""

$ServerNameFile = gc -path "C:\temp\Servers.txt"

$BGColorColumn = "#BFC3C4"

$BGColorOnline = "#6DC046"

$BGColorOffline = "#D43235"

$BGColorReportTotal = "#4AA7E1"

$SendEmail = "True"

$ReportOutFile = "C:\temp\ServerUptimeReport.html"



<#==============================

SMTP Settings

Edit with your email settings:

================================#>

$smtpsettings = @{

To = "Email add"

From = "Email add"

Subject = "DC Uptime Report"

SmtpServer = "add SMTP server"

}



<#============================

Do not edit below this line!

==============================#>



<#========

Counters

==========#>

$servername =0

$ServerCount = 0

$SuccessCount = 0

$UnreachableCount = 0



<#====================

HTML Report Settings

======================#>

$Report = "

<html>

<head>

<title> Server Uptime Report </title>

</head>

<body {background-color:#D7D8D8;}>

<H1 Align=`"Center`"> <B>SaleLytics Server Uptime Report </B></H1>

<H3 Align=`"Center`"> $(Get-Date -Format D) </H3>

<H3 Align=`"Center`"> $(Get-Date -Format T) </H3>

<table Border=`"1`" CellPadding=`"3`" Align=`"Center`">

<tr>

<td BGColor=$BGColorColumn Align=center><b> SERVER Name </b></td>

<td BGColor=$BGColorColumn Align=center><b> SERVER IP </b></td>

<td BGColor=$BGColorColumn Align=center><b> STATUS </b></td>

<td BGColor=$BGColorColumn Align=center><b> UPTIME </b></td>

</tr>"



<#========================

Query servers for uptime

==========================#>





ForEach($Server in $ServerNameFile) {


$servernames=Get-WmiObject Win32_Computersystem -ComputerName $server|Select-Object Name -ExpandProperty Name

$c=nslookup $server|Select-String Address

$c = $c -replace '\s',''

$dns = $c -split ':'

$g=$dns[3]

$OutputObj = New-Object -TypeName PSobject

$OutputObj | Add-Member -MemberType NoteProperty -Name ServerName -Value $Servernames

$OutputObj | Add-Member -MemberType NoteProperty -Name ServerIP -Value $g

$Status = 0

$ServerCount++

If(Test-Connection -Computer $Server -count 1 -ea 0) {

$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "Online"

try {

$BootTime = (Get-WmiObject win32_operatingSystem -computer $Server -ErrorAction stop).lastbootuptime

$BootTime = [System.Management.ManagementDateTimeconverter]::ToDateTime($BootTime)

$Now = Get-Date

$span = New-TimeSpan $BootTime $Now

$Days = $span.days

$Hours = $span.hours

$Minutes = $span.minutes

$Seconds = $span.seconds

<#===============================

Remove plurals if the value = 1

=================================#>

If ($Days -eq 1)

{$Day = "1 day "}

else

{$Day = "$Days days "}


If ($Hours -eq 1)

{$Hr = "1 hr "}

else

{$Hr = "$Hours hrs "}


If ($Minutes -eq 1)

{$Min = "1 min "}

else

{$Min = "$Minutes mins "}


If ($Seconds -eq 1)

{$Sec = "1 sec"}

else

{$Sec = "$Seconds secs"}


$Uptime = $Day + $Hr + $Min + $Sec





<#==================

Create Output List

====================#>

$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value $Uptime

$Status=1

$SuccessCount++

} catch {

$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "Query Failed"

<# Not currently reporting on this... #>

}


} else {

$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "Offline"

$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "Unreachable"

$UnreachableCount++

}



<#===============================================

Display output on screen and add to HTML report

=================================================#>


If($Status) {

$BGColor=$BGColorOnline

} else {

$BGColor=$BGColorOffline

}


$Report += "

<TR>

<TD BGColor=$BGColor Align = center>$($OutputObj.ServerName)</TD>

<TD BGColor=$BGColor Align = center>$($OutputObj.ServerIP)</TD>

<TD BGColor=$BGColor Align = center>$($OutputObj.Status)</TD>

<TD BGColor=$BGColor Align = center>$($OutputObj.Uptime)</TD>

</TR>"

}



<#====================

Assemble HTML Report

======================#>

$Report +="

</table>

<br>

<table Border=`"1`" CellPadding=`"3`" Align=`"Center`">

<tr>

<td BGColor=$BGColorReportTotal Align = right>Servers Scanned: </td>

<td BGColor=$BGColorReportTotal Align = right>$ServerCount</td>

</tr>

<tr>

<td BGColor=$BGColorOnline Align = right>Servers Online: </td>

<td BGColor=$BGColorOnline Align = right>$SuccessCount</td>

</tr>

<tr>

<td BGColor=$BGColorOffline Align = right>Servers Offline: </td>

<td BGColor=$BGColorOffline Align = right>$UnreachableCount</td>

</tr>

</table>

</body>

</html>"



$Report | Out-File $ReportOutFile -Force





<#==============================================================================================================

Email HTML Report

==============================================================================================================#>

IF ($SendEmail -eq "True")

{

Send-MailMessage @smtpsettings -Body $Report -BodyAsHtml

}



 
 
 

Recent Posts

See All
PowerShell - List All Domain SPNs

Save as same List_ALL_SPNs.ps1 or similar, this LDap calls the Domain for all Service Principal names and accounts related #Build LDAP...

 
 
 
PowerShell - Start-Monitoring

This is a great script used to Monitor and Email if a server is up or down, once ran, and smtp and from address is set, then run:...

 
 
 

Comments


Post: Blog2 Post
  • Facebook
  • Twitter
  • LinkedIn

©2022 by Boyette Technical Services. Proudly created with Wix.com

bottom of page