1

I have a powershell script I want to use to monitor the size of a file (at a certain interval) and send me an email alert if the file reaches a certain size. However, after it discovers that the file has reached that size, the powershell script exits.

Presently the script operates in a for loop, with the file-size check happening first, and if it hasn't passed the maximum size, it will wait for the given time-interval and check the file size again. But I'm thinking about changing this for loop to a do...while so that it waits the interval of time first and then checks the size of the file.

But the script will still exit when the email is sent.

Is there any way to ensure that the script will be re-run when it exits after the email is sent?

I read that there are processes in database systems that monitor other processes to make sure that they are running, and if they are not, the watching process re-spawns the process that died.

leeand00
  • 4,869
  • 15
  • 69
  • 110

1 Answers1

3

It sounds like your email sending function is outside the loop, and is followed by either an exit statement or the end of the script.

Can you just put the email sending function within the loop? You'll probably need to add a counter that will establish a time or repetition-based hold so that it doesn't immediately get in a loop of re-emailing you, but that's not too hard.

If you do that, then the script won't exit the loop at all: it'll send and email and go back to checking for the file.

Bob
  • 597
  • 2
  • 8