2

I have to write a script which tries to ping a device, then if this device is not responsive for X days... then a mail is generated and the X days counter for the device is reset.

What would be the easiest way to achieve this? I was thinking of a CSV/XML file where I'd store the last day when the device was responsive and if X days has passed then a mail should be generated and the date will be reset.

Am I missing any obvious way to tackle this problem? I was also thinking about making a infinite loop where I am running pings every 1h and where I maintain all the devices in a hashtable.. but not sure about the consequences of running an infinite loop

thanks!

halv
  • 121
  • 1
  • 6
  • This is a task much better suited to a network monitoring tool, not a home-grown long-running script. – alroc Oct 08 '14 at 11:16

2 Answers2

0

I would create a scheduled task that runs nightly and pings the device, then logs the success or failure. Every time it runs, it would count the number of failures in the log. If that was X number of times, send email. Do this every day until a success, then clear the log file to start over.

Samuel Prout
  • 655
  • 5
  • 16
0

If you properly pause execution of your script (like with Start-Sleep), running an infinite loop is not a problem. Only drawback would be restarting your script when you change any code but it's not really a problem either.

Since you're gonna need to serialize your hashtable to a file (probably to a CSV or XML, but there are different ways), you can also run your code as a Scheduled Task every hour which reads the previous data on every launch, pings the devices, sends emails, saves new results back and stops.

If you choose a human friendly file format, you can also edit your file to add or remove devices easily unless you populate your hashtable from another place.

Be sure to add appropriate logging and exception handling. Also, make sure that your code handles DateTime and time differences correctly after reading from file by manually checking what Powershell sees.

Community
  • 1
  • 1
Furkan Omay
  • 1,047
  • 1
  • 11
  • 22