Audit uptime of all servers

Audit uptime of all servers

Monitoring servers is an important thing, almost uptime of all servers.  You should have some dedicated tools such as Zabbix, Nagios, etc… BUT ! Here is a script to generate an HTML report of all your servers uptime.

Those servers are all registered into AD and the ‘get’ is based on the operating system detected by AD.


#Get-Update - Return lastboottime and update - input : servername
Function Get-Uptime { 
    param ( 
        $Credential = [System.Management.Automation.PSCredential]::Empty 
    #Need to verify that the hostname is valid in DNS 
    process { 
        foreach ($Computer in $ComputerName) { 
            try { 
                $hostdns = [System.Net.DNS]::GetHostEntry($Computer) 
                $OS = Get-WmiObject win32_operatingsystem -ComputerName $Computer -ErrorAction Stop -Credential $Credential 
                $BootTime = $OS.ConvertToDateTime($OS.LastBootUpTime) 
                $Uptime = $OS.ConvertToDateTime($OS.LocalDateTime) - $boottime 
                $propHash = [ordered]@{ 
                    ComputerName = $Computer
                    BootTime     = $BootTime
                    Uptime       = $Uptime
                $objComputerUptime = New-Object PSOBject -Property $propHash 
            catch [Exception] { 
                #Write-Output "$computer $($_.Exception.Message)"
                           $propHash = [ordered]@{ 
                    ComputerName = $Computer
                    BootTime     = "Unavailable"
                    Uptime       = "Unavailable"
                $objComputerUptime = New-Object PSOBject -Property $propHash 



#Deleting all result files
If(Test-Path $OutputFile){
    Remove-item $OutputFile -confirm:$false -force
If(Test-Path $UptimeHTMLReport){
    Remove-item $UptimeHTMLReport -confirm:$false -force

Add-Content -path $OutputFile -value "ServerName,LastBootTime,Uptime"

#Fetching list of all Servers in AD
$Serverlist=Get-ADComputer -Properties * -filter {OperatingSystem -like "*Server*"}

    $htmltableheader = '
<style type="text/css">
.myTable { background-color:#FFFFE0;border-collapse:collapse;font-family:Georgia, Garamond, Serif;color:black; }
.myTable th { font:bold 18px/1.1em Arial, Helvetica, Sans-Serif;text-shadow: 1px 1px 4px black;letter-spacing:0.3em;background-color:#BDB76B;color:white; }
.myTable td, .myTable th { padding:5px;border:1px solid #BDB76B; }
.myTable td { line-height:2.5em; }
<h3 align="Center">Uptime Check Status</h3>
                        <table class="myTable" align="Center">
                        <th align="Center" width="60">Status</th>
                        <th align="Center">Server</th>
                        <th align="Center">LastBoot</th>
                        <th align="Center">Uptime</th>
Add-content $UptimeHTMLReport -value $htmltableheader

$imgsrc='<img src =""  width="40" height="40">'
$ImageKO='<img src ="" width="40" height="40">'
$ImageOK='<img src ="" width="40" height="40">'
$ImageAVG='<img src ="" width="40" height="40">'
$ServerList | Foreach-object{

    Write-host "$ServerName - " -nonewline
    $GetUptime=Get-Uptime $ServerName
    If($GetUptime.Uptime -ne "Unavailable"){
    If($GetUptime.Uptime -eq "Unavailable"){
    Add-content -path $OutputFile -value "$ServerName,$LastBoot,$Uptime"
       If(($UptimeDays -gt 90) -and ($UptimeDays -lt 150) -and ($Uptime -ne "Unavailable")){
             Add-Content -path $UptimeHTMLReport -value "<tr>
                            <td align='Center'>$ImageAVG</td>
                                               <td align='Center' bgcolor=yellow>$ServerName</td>
                                               <td align='Center' bgcolor=yellow>$LastBoot</td>
                                               <td align='Center' bgcolor=yellow>$UptimeDays</td>
       If(($UptimeDays -ge 150) -and ($UptimeDays -ne "Unavailable")){
        Write-host "$UptimeDays" -foregroundcolor:Red
             Add-Content -path $UptimeHTMLReport -value "<tr>
                            <td align='center' >$imgsrc</td>
                                               <td align='Center' bgcolor=red>$ServerName</td>
                                               <td align='Center' bgcolor=red>$LastBoot</td>
                                               <td align='Center' bgcolor=red>$UptimeDays</td>
       If(($UptimeDays -lt 90) -and ($UptimeDays -ne "Unavailable")){
             Write-host "$UptimeDays"
             Add-Content -path $UptimeHTMLReport -value "<tr>
                            <td align='Center'>$ImageOK</td>
                                               <td align='Center' bgcolor=green>$ServerName</td>
                                               <td align='Center' bgcolor=green>$LastBoot</td>
                                               <td align='Center' bgcolor=green>$UptimeDays</td>
       If($UptimeDays -eq "Unavailable"){
             Write-host "Unavailable" -foregroundcolor:yellow -backgroundcolor:blue
             Add-Content -path $UptimeHTMLReport -value "<tr>
                            <td align='Center'>$ImageKO</td>
                                               <td align='Center'>$ServerName</td>
                                               <td align='Center'>$LastBoot</td>
                                               <td align='Center'>N/A</td>

Add-Content -path $UptimeHTMLReport -value "</table>"

Write-host "OK" -foregroundcolor:Green
$htmlreport=Get-content $UptimeHTMLReport | out-string
Send-MailMessage -SmtpServer $SmtpServer -To $MailTo -From $MailFrom -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -subject "[UPTIME] Audit Servers" -Attachments $UptimeHTMLReport

Don’t forget to modify those lines :

$OutputFile=”C:\Scripts\Tools\GetAllServersUptime.csv” $UptimeHTMLReport=”C:\Scripts\Tools\UptimeCheckServer.html” $MailTo=””



And enjoy !

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site