$ServerNameOption = ""
$ServerNameFile = gc -path "C:\users\bharalid\desktop\Servers.txt"
$BGColorColumn = "#BFC3C4"
$BGColorOnline = "#6DC046"
$BGColorOffline = "#D43235"
$BGColorReportTotal = "#4AA7E1"
$SendEmail = "True"
$ReportOutFile = "C:\users\bharalid\desktop\ServerUptimeReport.html"
<#==============================
SMTP Settings
Edit with your email settings:
================================#>
$smtpsettings = @{
To = ""
From = ""
Subject = ""
SmtpServer = ''
}
<#============================
Do not edit below this line!
==============================#>
<#========
Counters
==========#>
$servername =0
$ServerCount = 0
$SuccessCount = 0
$UnreachableCount = 0
<#====================
HTML Report Settings
======================#>
$Report = "
Server Uptime Report
Server Uptime Report
$(Get-Date -Format D)
$(Get-Date -Format T)
SERVER Name |
SERVER IP |
STATUS |
UPTIME |
"
<#========================
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 += "
$($OutputObj.ServerName) |
$($OutputObj.ServerIP) |
$($OutputObj.Status) |
$($OutputObj.Uptime) |
"
}
<#====================
Assemble HTML Report
======================#>
$Report +="
Servers Scanned: |
$ServerCount |
Servers Online: |
$SuccessCount |
Servers Offline: |
$UnreachableCount |
"
$Report | Out-File $ReportOutFile -Force
<#==============================================================================================================
Email HTML Report
==============================================================================================================#>
IF ($SendEmail -eq "True")
{
Send-MailMessage @smtpsettings -Body $Report -BodyAsHtml
}