Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

AUTOMATE SERVER UPTIME PERCENTAGE AND SEND TO E_MAIL

$
0
0

Good afternoon, I am trying to create a power shell script to give me a % of up time on production servers

I was able to run the script and it shows good output on the screen but sends nothing to e-mail.

It also seems it overwrites the $body with the last entry.

$body =
#>
#$Diff = 86400000*$DaysAgo # Milliseconds in a day
$startDate = get-date $(get-date).AddMonths(-1).ToString("01/MM/yyyy")
$endDate = $startDate.AddMonths(1).AddMilliseconds(-1)

$XMLFilter = "<QueryList>
<Query Id=`"0`" Path=`"System`">
<Select Path=`"System`">*[System[( (EventID = 6005 or EventID = 6006) ) and TimeCreated[@SystemTime&gt;='
$($startDate.tostring("yyyy-MM-dd"))T00:00:00.000Z' and @SystemTime&lt;='$($endDate.tostring("yyyy-MM-dd"))T22:59:59.999Z']]]</Select>
</Query>
</QueryList>"

# Get the list of tartget servers
[string[]]$Computers = "SERVER1", "SERVER2", "SERVER3"  | Sort-Object ComputerName
# Foreach target server
ForEach ($computer in $Computers) {
# Get the reboot info from event log on current target server
$RebootEvents = get-winevent -FilterXML $XMLFilter -computername $Computer -ErrorAction SilentlyContinue -ErrorVariable FailedComputer | sort-object -Descending -property TimeCreated
$down = $null
$up = $null
[timespan]$TotalDownTime = New-TimeSpan
Foreach($rebootEvent in $RebootEvents) {
if ($rebootEvent.ID -eq 6006) {
$down = $rebootEvent.TimeCreated
} #end if eventID
Else {
$up = $rebootEvent.TimeCreated
} #end else
if($down -AND $up) {
if($down -ge $up) {
Write-Host -foregroundColor Red "*** Invalid data. Ignoring $($up)"
$up = $down
}
#end if down is greater than up
[timespan]$CurrentDownTime = new-TimeSpan -start $down -end $up
$TotalDownTime += $currentDownTime
$down = $null
$up = $null
} #end if down and up  
} #end foreach

$periodDifference = $endDate - $startdate
$minutesInMonth = $diff.mins
$minutesInDay = 24*60
$percentUpTime = (100 - ($TotalDownTime.TotalMinutes/$periodDifference.TotalMinutes)*100)
"$computer $($percentUptime.ToString("###.###")) % "
}


# Create column headers of Table1
$HtmlTable1 = "<table border='1' align='Left' cellpadding='3' cellspacing='15' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #FFFFFF'>
<th align=left><b>Computer Name</b></th>
<th align=left><b>Percent Of Uptime</b></th>
</tr>"

# Insert data into Table1
foreach ($row in $body)

    $HtmlTable1 += "<tr style='font-size:13px;background-color:#FFFFFF'>
    <td>" + $row.$Computer + "</td>
    <td>" + $row.$percentUptime + "</td>
    </tr>"
}
$HtmlTable1 += "</table>"



# Send Mail Inputs
$smtpserver = "mail.test.com"
$from = "Powershell Mail <notifications@test.com>"
$to = "<test1@test.com.com>"
$cc = "<test2@test.com>"
$subject = "Prodution Server Uptime Percent"
$body = "Production Server Uptime Percent:<br/><br/>" + $HtmlTable1

Send-MailMessage -smtpserver $smtpserver -from $from -to $to -cc $cc -subject $subject -body $body 



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>