Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check windows service status and email if stopped using power shell script
$client = "Test"
$service=""
$EmailBody = ""
$EmailSubject = ""
$SMTPServer = "relay2"
$PCName = $env:COMPUTERNAME
$EmailFrom = "$client <Test@cshandler.com>"
$EmailTo = "achoudhary@courtalert.com"
# Check diary sender service status
$service="CourtAlert Diary Sender"
$result = Get-Service | Where-Object {$_.status -eq "stopped"} | findstr /c:"$($service)"
if(![string]::IsNullOrEmpty($result))
{
$EmailBody= "The '$($service)' service has stopped on $($PCName)."
$EmailSubject = "$($service) important alert"
Write-host "Sending Email alert for $($service)"
Write-Host $EmailBody
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer -Priority High
}
$service="CourtAlert Mail Relay"
$result = Get-Service | Where-Object {$_.status -eq "stopped"} | findstr /c:"$($service)"
if(![string]::IsNullOrEmpty($result))
{
$EmailBody= "The '$($service)' service has stopped on $($PCName)."
$EmailSubject = "$($service) important alert"
Write-host "Sending Email alert for $($service)"
Write-Host $EmailBody
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer -Priority High
}
$service="CourtAlert ECF"
$result = Get-Service | Where-Object {$_.status -eq "stopped"} | findstr /c:"$($service)"
if(![string]::IsNullOrEmpty($result))
{
$EmailBody= "The '$($service)' service has stopped on $($PCName)."
$EmailSubject = "$($service) important alert"
Write-host "Sending Email alert for $($service)"
Write-Host $EmailBody
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer -Priority High
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.