Simple Powershell Scripts pt.2

This is a simple script I wrote today to send an email when a specific service is no longer running. I’ve been having issues with Veeam randomly stopping the SQL service and I built this to send, on a scheduled task, a message via Google’s open SMTP and an app password on my MFA protected domain alerts account:

## Some default params setting which service to look at, 
## push the app password into a string, 
## set the outgoing email, and save those as a cred file
$sqlstatus = Get-Service "XXXSERVICEXXX" | Select-Object -Property Status
$pwd = ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "XXX@EMAIL.com",$pwd
## splating my params
$param = @{
    SMTPServer = "smtp.gmail.com"
    Port = 587
    UseSsl = $true
    Credential = $cred
    From = "XXX@EMAIL.com"
    To = "XXX@EMAIL.com"
    Subject = "Service no longer running!"
    Body = "Service no longer running!"
}
## If status isn't running, let us know!
if(!($sqlstatus.Status -eq "Running")){    
    Send-MailMessage @param
    }

This script is less than ideal because I’m using plaintext passwords in the script. For critical accounts, do not use this! Find a better way to authenticate to your SMTP server or build in functions into the script to connect to your password manager API. Don’t run this on a shared computer and follow best practices to keep this password secured from prying eyes!

Leave a comment