If a script fails because computer is shut down, or the script thinks it’s working fine but isn’t, how to trigger fail alert?

1

I am currently using PowerShell and Task Scheduler to automate a task for the business where I currently work. This script updates a list and will ideally be run every 10 minutes. The computer that we use to run this recurring script can be a little janky and will randomly shut down. I'm trying to find a way to get alerts if computer running the script is offline.

The main way I'm thinking to do this would be to setup a notification for each time the program runs, then have a second script check the notification. If there is a missing notification indicating the script didn't run, it sends an alert. I found an answer that takes me partway to an answer but I don't think it would work if the machine shuts down.

Make Windows Task Scheduler alert me on fail

Thank you for your time helping with this question.

HankMorgan

Posted 2020-02-03T16:04:17.770

Reputation: 13

1I think that most of the solutions to this problem would require a second computer, in which case it would be better just to move the original script to that computer. An alternative might be to save a log file containing when the script ran to cloud storage that you can check from anywhere. – Worthwelle – 2020-02-03T16:12:37.247

Answers

1

I agree with other comments. You would need a second machine running a connectivity test or something of the sort to the machine that is running the other code to detect a failure. Here is a sample powershell script that would email you if it did not get a successful ping response back from the other machine. Following code assumes you use 0ffice365 mail/exchange online but can be altered to whatever you use.

$testComputer = ping 1.1.1.1 -n 1 | select-string "Sent"  #replace the 1.1.1.1 with your server that fails

if($testComputer -match 'Sent = 1')
{
    write-host("Connection to computer running scheduled task is good.")
}
else
{
    write-host("Connection to computer running scheduled task has failed. Check to see if it is offline.")
    $computerParamsFailed = "Connection to computer running scheduled task has failed. Check to see if it is offline." 

    $username="user@domain.onmicrosoft.com"  #replace with email
    $password=ConvertTo-SecureString "PASSWORDHERE"-AsPlainText -Force  #replace what is in quotes with your email password
    $mycredentials = New-Object System.Management.Automation.PSCredential ($username, $password)

    Send-MailMessage -To to@domain.onmicrosoft.com -subject "Computer has gone offline." -body $computerParamsFailed -UseSsl -Port 587 -SmtpServer smtp.office365.com -From $username -Credential $mycredentials
    #change "to" email value 
}

This can be set to run every 10 minutes on another machine. If it detects a connection, it will do nothing. If it does not get a response, it will send you an email.

Narzard

Posted 2020-02-03T16:04:17.770

Reputation: 2 276

1

I agree with Worthwelle. There would need to be another machine monitoring the status of the machine where that script was being run. In this case, if there was a machine that was reliable enough to monitor the first machine, then you might as well just run the script on that machine instead. Best solution... correct the issue causing machine running the script to crash or simply replace it.

ap0th0cary

Posted 2020-02-03T16:04:17.770

Reputation: 21