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.

1
  • 1
    I 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
    Commented Feb 3, 2020 at 16:12

2 Answers 2

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="[email protected]"  #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 [email protected] -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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .