I have got one script which actually checks all servers are able to ping or not, it creates excel file with the UP and Down status of servers and later it sends email if any server is down. I have modified script according to my needs.
I have used task scheduler to run this every 5 mins.
Now problem with this script is, It sending the email every 5 mins even there is no server down.
But my requirement is it should send email whenever any server is down otherwise mail sending should be skip.
#Below is the script
#$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content “E:\Reports\Ping\Ppassword.txt”
$emailusername = “myemail@domain.com”
$encrypted = Get-Content “E:\Reports\Ping\Ppassword.txt” | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
$PingResultPath = “E:\Reports\Ping\PingResults.xls”
$ResultExcel = new-object -comobject excel.application
if (Test-Path $PingResultPath)
{
$ResultWorkbook = $ResultExcel.WorkBooks.Open($PingResultPath)
$ExcelWorksheet = $ResultWorkbook.Worksheets.Item(1)
}
else {
$ResultWorkbook = $ResultExcel.Workbooks.Add()
$ExcelWorksheet = $ResultWorkbook.Worksheets.Item(1)
}
$ResultExcel.Visible = $True
$downServerArray = [System.Collections.ArrayList]@()
$ExcelWorksheet.Cells.Item(1, 1) = “MachineName”
$ExcelWorksheet.Cells.Item(1, 2) = “Ping Result”
$Servers = gc “E:\Reports\Ping\ServerList.txt”
$count = $Servers.count
$ExcelWorksheet.Cells.Item(1,1).Interior.ColorIndex = 6
$ExcelWorksheet.Cells.Item(1,2).Interior.ColorIndex = 6
$row=2
$Servers | foreach-object{
$pingResult=$null
$Server = $_
$pingResult = Test-Connection $Server -Count 1 -ea silentlycontinue
if($pingResult){
$ExcelWorksheet.Cells.Item($row,1) = $Server
$ExcelWorksheet.Cells.Item($row,2) = “UP”
$ExcelWorksheet.Cells.Item($row,1).Interior.ColorIndex = 17
$ExcelWorksheet.Cells.Item($row,2).Interior.ColorIndex = 4
$row++}
else {
$ExcelWorksheet.Cells.Item($row,1) = $Server
$ExcelWorksheet.Cells.Item($row,2) = “DOWN”
$ExcelWorksheet.Cells.Item($row,1).Interior.ColorIndex = 17
$ExcelWorksheet.Cells.Item($row,2).Interior.ColorIndex = 3
$arrayID = $downServerArray.Add($Server)
$row++}
}
Format the excel
To wrap the text
$d = $ExcelWorksheet.UsedRange
$null = $d.EntireColumn.AutoFit()
##to set column width and cell alignment
$ExcelWorksheet.Columns.Item(1).columnWidth = 24
$ExcelWorksheet.Columns.Item(2).columnWidth = 24
$ExcelWorksheet.Columns.HorizontalAlignment = -4131
$ExcelWorksheet.rows.item(1).Font.Bold = $True
##to apply filter
$headerRange = $ExcelWorksheet.Range(“a1”,“n1”)
$headerRange.AutoFilter() | Out-Null
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$enddate = (get-date -f yyyy-MM-dd).tostring()
$timeStamp= get-date -f MM-dd-yyyy_HH_mm_ss
$ResultExcel.ActiveWorkbook.SaveAs( (get-location).tostring() +“" +$timeStamp +”.xlx", $xlFixedFormat)
$ResultExcel.Workbooks.Close()
$ResultExcel.Quit()
Write-Host ’ Number of Servers that are down at the moment : ’ $downServerArray.Count
if($downServerArray.Count -gt 0)
{
$Body =‘body{background-color:lightgrey;}’
$Body = $Body + " Hi Team,
The below mentioned servers are down at the moment. Please check on these servers.
"
$Body = $Body + "
"
foreach($item in $downServerArray)
{
Write-Host $item -ForegroundColor red -BackgroundColor white
$Body = $Body + " "
}
$Body = $Body + “
Regards,
IT Team”
$SmtpServer = ‘smtp.office365.com’
$MailtTo = ‘myemail@domain.com’
$MailFrom = ‘myemail@domain.com’
$Mailcc = ‘myemail@domain.com’
$MailSubject = “Ping Status Result”
Send-MailMessage -To “$MailtTo” -cc “$Mailcc” -from “$MailFrom” -Subject $MailSubject -Body “$Body” -BodyAsHtml -SmtpServer $SmtpServer -UseSsl -Credential $Credentials -port 587
Let me know anyone have any suggestions.
Regards,
Ram