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.

#Function

#Get-Update - Return lastboottime and update - input : servername
Function Get-Uptime { 
    [CmdletBinding()] 
    param ( 
        [Parameter(Mandatory=$false, 
                        Position=0, 
                        ValueFromPipeline=$true, 
                        ValueFromPipelineByPropertyName=$true)] 
        [Alias("Name")] 
        [string[]]$ComputerName=$env:COMPUTERNAME, 
        $Credential = [System.Management.Automation.PSCredential]::Empty 
        ) 
 
    begin{} 
 
    #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 
                $objComputerUptime 
                }  
            catch [Exception] { 
                #Write-Output "$computer $($_.Exception.Message)"
                           $propHash = [ordered]@{ 
                    ComputerName = $Computer
                    BootTime     = "Unavailable"
                    Uptime       = "Unavailable"
                    } 
                $objComputerUptime = New-Object PSOBject -Property $propHash 
                $objComputerUptime
                #return 
                } 
        } 
    } 
    end{} 
}

#Variables
$OutputFile="C:\Scripts\Tools\GetAllServersUptime.csv"
$UptimeHTMLReport="C:\Scripts\Tools\UptimeCheckServer.html"
$MailTo=""
$MailFrom=""
$SmtpServer=""


#MAIN

#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; }
</style>
<h3 align="Center">Uptime Check Status</h3>
                        <p>
                        <table class="myTable" align="Center">
                        <tr>
                        <th align="Center" width="60">Status</th>
                        <th align="Center">Server</th>
                        <th align="Center">LastBoot</th>
                        <th align="Center">Uptime</th>
                        </tr>'
Add-content $UptimeHTMLReport -value $htmltableheader

$imgsrc='<img src ="http://apps.campusops.oregonstate.edu/emergency/images/EmerPrep/alertlight.png"  width="40" height="40">'
$ImageKO='<img src ="https://openclipart.org/download/285479/Thumbs-down-Circle.svg" width="40" height="40">'
$ImageOK='<img src ="https://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Green_check.svg/480px-Green_check.svg.png" width="40" height="40">'
$ImageAVG='<img src ="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTRN2qS4vBKcDpgdIcxMOIVWDcoxS0w8a6t8jV62zdXKyMIhFRXPw" width="40" height="40">'
#Processing
$ServerList | Foreach-object{

    $Servername=$_.Name
    Write-host "$ServerName - " -nonewline
    $GetUptime=Get-Uptime $ServerName
    If($GetUptime.Uptime -ne "Unavailable"){
        $LastBoot=$GetUptime.BootTime
        $Uptime=$GetUptime.Uptime
           [int]$UptimeDays=$GetUptime.Uptime.Days
    }
    If($GetUptime.Uptime -eq "Unavailable"){
        $LastBoot="Unavailable"
        [string]$Uptime="Unavailable"
           [string]$UptimeDays="Unavailable"
    }
    Add-content -path $OutputFile -value "$ServerName,$LastBoot,$Uptime"
       If(($UptimeDays -gt 90) -and ($UptimeDays -lt 150) -and ($Uptime -ne "Unavailable")){
             $UptimeDays=$Uptime.Days
             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>
                                               </tr>"
       }
       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>
                                               </tr>"
       }
       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>
                                               </tr>"
       }
       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>
                                               </tr>"
       }
}

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=””

$MailFrom=””

$SmtpServer=””

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