Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Created January 13, 2017 22:07
Show Gist options
  • Save vendettamit/6b1fe7d0e4c45f39cb596526af7bbcfd to your computer and use it in GitHub Desktop.
Save vendettamit/6b1fe7d0e4c45f39cb596526af7bbcfd to your computer and use it in GitHub Desktop.
Check windows service status and email if stopped using power shell script
$client = "Test"
$service=""
$EmailBody = ""
$EmailSubject = ""
$SMTPServer = "relay2"
$PCName = $env:COMPUTERNAME
$EmailFrom = "$client <[email protected]>"
$EmailTo = "[email protected]"
# 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