We have a series of PowerShell scripts that automate tasks in SharePoint. These scripts are all run via task scheduler tasks every 5 minutes and ran without hiccups for about a year.
In an effort to reduce redundancy and scale our code base, we decided to pull out common code into a Main.ps1 file (essentially just wrapper code that all the scripts utilize) and then use dot-source notation to include the primary set of code for each child script. We could obviously avoid using dot-source notation by having all the code live in Main.ps1, but we would prefer not to do that. The way it works is that we pass in a param to Main.ps1 to specify which script to execute. You can also pass in multiple params to specify multiple scripts to execute.
Ever since doing this, we've noticed that the task scheduler tasks will randomly miss executions. The code in the tasks literally only takes around 30 seconds to execute (and that is a max run time for the most complex script). I incorporated some logging and it looks like the script will just hang on a random part of the code, anywhere from a few minutes up until 10 minutes - really weird - and only happens maybe once or twice per day. Due to this, I re-created the tasks with a Priority of 5 (by exporting the tasks, changing the Priority in the XML, and re-importing the tasks). This didn't seem to solve the problem as the problem has still persisted.
It could very well be the case that the SharePoint environment is being heavily taxed and for whatever reason the tasks can't run smoothly - although I'm not totally sold on this.
Here are my questions:
- Is there known issues with task scheduler tasks hanging when using dot-source notation to include scripts? I also understand that modules could be used, but let's keep this out of the discussion for now, if possible.
- Should I be setting the tasks to run as Priority 1 tasks?
- Is there any other ways to debug this problem?
I also don't want to kill the tasks if the next scheduled run is about to execute and the script is running - because the script could be in the middle of an important transaction.
Any input would be appreciated.