I wrote a script to check for Windows Services that are in a hung state and send an email to the user accordingly with the subject line.
- I want to send the email with an image or table data consisting of the Server Name, Service Name, Service Start Type, and Service Status, but right now I am able to send the email with just the body text.
How can I send the email with the data consisting of the Server Name, Service Name, Service Start Type, and Service Status as a table in the below format?
#AUTHOR: Chandrakanth Reddy
#DATE: 8/15/2021
#Email Details
$FromAddress = 'XYZ.com'
$ToAddress = 'XYZ.com'
$SmtpServer = 'ABC.com'
#Machine to be monitored
$Computer = "XXXX"
#Create a subset of the previous array for services you want to monitor
$ServiceName = "WSearch"
Foreach ($ServerName in $Computer) {
Foreach ($Service in $ServiceName) {
#Get current status
$CurrentStatus = Get-Service -Name $ServiceName -ComputerName $Computer #| Where-Object { $_.Status -eq "StartPending" }
if ($_.Status -eq "StartPending") {
#$CurrentStatus = 'StartPending'
If (($FromAddress) -And ($SmtpServer)) {
$msgBody = $ServiceName + " " + "Windows Service is" + " " + $CurrentStatus +" " + "on" + " " + $Computer
Send-MailMessage -from $FromAddress -to $FromAddress -SmtpServer $SmtpServer -Subject "$Computer - $ServiceName Windows Service is in Hung state '$CurrentStatus' " -BodyAsHtml $msgBody
}
}
elseif ($_.Status -eq "StopPending") {
#$CurrentStatus = 'StopPending'
If (($FromAddress) -And ($SmtpServer)) {
$msgBody = $ServiceName + " " + "Windows Service is" + " " + $CurrentStatus +" " + "on" + " " + $Computer
Send-MailMessage -from $FromAddress -to $ToAddress -SmtpServer $SmtpServer -Subject "$Computer $ServiceName is not running" -BodyAsHtml $msgBody
}
}
elseif ($_.Status -eq 'Running' -or $_.Status -eq 'Stopped' -or $_.Status -eq 'ContinuePending') {
Exit
}
}
}
Exit
Format-Table
.