0

I'm trying to automate a lengthy process that can be broken down into several steps. (say Steps 1-5)

I have written a script that separates these into functions and call them sequentially.

However, we now have the additional requirement of making the script restartable. That is, if it fails in any one of the steps, rerunning the script would cause it to skip all completed steps and retry from the failed one.

Is this at all possible without referencing an external log file?

I've tried using workflows but it seems like recursion isn't supported.

Any ideas?

7
  • is there a reason you can't keep an external log file? it seems like the easiest solution to your issue. Commented Nov 6, 2017 at 14:13
  • Definitely agree with you there. The question is more for educational purposes but you're probably right. Commented Nov 6, 2017 at 14:15
  • Use a try/catch on each function call that starts the first function call if a failure occurs. Commented Nov 6, 2017 at 14:20
  • @TheIncorrigible1 Thanks for that. Sorry, I should have made it more clear. The script has to terminate when it encounters a failure as this will allow us to fix any external issues before restarting the script on demand. It looks like your suggestion will cause the script to run perpetually. Commented Nov 6, 2017 at 14:27
  • Then just have it recall the script in a new scope in the catch block. Commented Nov 6, 2017 at 14:33

1 Answer 1

1

Some options aside from using a log file.

  • Use the registry

you can set a registry value to a number depending on what step you stopped on, this removes the need for a log file but is somewhat similar in terms of 'external' storage

  • Check the task status on each run

depending on the tasks you could have the script 'test', for example, step 3 to see if it has already been completed, then check step 4, 5 etc. until it encounters one it needs to run and continue from there, this may be impossible or require a lot of overhead code though for not much payoff.

  • Allow the user to continue from within the script.

this is probably the best way of doing it (aside from just using a log file), run the script in blocks, and when an error is encountered you can prompt the user to fix the issue before pressing 'enter' to re-run the previous script block, this makes it easy to provide information about what failed as well.

the main thing here is that once a script 'quits', in order to know what happened in it's last run it needs an external source of information, or to handle it in another way.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this @ConnorLSW. The last line in particular is so rudimentary that it bothers me that I thought that I can do it any other way. Cheers!
@Yad well it's mabye not the most obvious thing, other languages have things like compiled settings or 'default' settings which can be changed and saved, (they are mostly just external files technically though) but powershell doesn't even have an attempt at such a feature, good luck with getting it working anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.