0

I have a specific scheduled task (one of several) that runs on a Server 2019 server. This scheduled task runs a Windows PowerShell script that interacts with a couple of REST APIs and logs to a file.

I have logged into the server as the user responsible for running the task and run the script without issue. When the script is run by the task scheduler, after about 19 minutes, execution stops. In Task Manager, I can see that the PowerShell process is still running but the amount of memory consumed drops to near zero and there is no more CPU activity. I can also see that writing to the log file stops.

When I right-click on the scheduled task and select "End", the process's memory/CPU utilization increases, logging resumes from where it left off, and the script completes as expected.

At the time the script stops, it is in a foreach loop. I wrote...a lot of logging output...so I can see that the last command in the loop was run. When I "end" the scheduled task, the first command at the beginning of the loops is run.

Any ideas how I might determine why the script stops?

StackExchangeGuy
  • 131
  • 1
  • 12
  • Does the output show that it exists the loop? – Sorean Apr 16 '20 at 16:56
  • No, the loop continues from the stopping point (in the middle of the array), after I "end" the scheduled task. – StackExchangeGuy Apr 16 '20 at 17:35
  • That sounds like a result is causing a problem during automation. It would be beneficial to see the code in your script. – Sorean Apr 16 '20 at 19:22
  • The script relies on a couple of PS modules I wrote, to populate some variables (outside the foreach loop, in which it stops). The script is too long, so I uploaded it to https://github.com/wetling23/redditPost. – StackExchangeGuy Apr 16 '20 at 20:41
  • In this example, we are working on customer 421 out of 468. This customer has no RMM or antivirus computers so lines 148 and 159 return nothing. Then, we skip the if/else statement that starts on line 166. When the job "hangs", the last line of the log file is from the "Get-AntiVirusComputer" command (line 159) saying that there are zero computers. The first line of the log file, after ending the scheduled task is "===============================================" and we move on to customer 422. – StackExchangeGuy Apr 16 '20 at 20:43
  • Are there other customers where $customerRmmDevices = $null or $customerAntiVirusDevices = $null stay as null values? Does it specifically fail on 421 every time? What happens if you start your loop at customer 400 instead of the whole array? – Sorean Apr 16 '20 at 21:54
  • Yes, before the task "hangs", there are several customers with zero RMM and antivirus devices. I added a skip for customer 421, in the loop and got the same "hang". It seems to be 18-19 minutes of run time, then it stops, regardless of which customer that is. – StackExchangeGuy Apr 16 '20 at 22:01
  • That sounds like it may be a timeout issue or a "kill task if running longer than X time" issue. – Sorean Apr 16 '20 at 22:22
  • But the task doesn't get killed. In the original post, I noted that the PowerShell process that runs the script is still running, it just isn't doing anything. I can see it in Task Manager. – StackExchangeGuy Apr 16 '20 at 22:37
  • You could try adding a timeout for each customer? Like if it takes more than 2 minutes, have it skip to the next customer? Sadly, you'll have to wait 20 minutes to find out if it skips and continues on or not. – Sorean Apr 16 '20 at 23:20
  • Okay, but where? I can't tell what is causing execution to stop, so how would I trigger "continue" – StackExchangeGuy Apr 17 '20 at 20:20
  • Put it around all the processing in the For Each loop. Start on line 130 and have it wrap all the way to the end of line 208. https://www.reddit.com/r/PowerShell/comments/82fl6g/how_do_you_set_timeouts_on_cmdlets/ – Sorean Apr 18 '20 at 01:34
  • I added the timer (https://github.com/wetling23/redditPost/blob/master/script.ps1) but the job still stopped at about 19 minutes. – StackExchangeGuy Apr 19 '20 at 19:02
  • Sorry, I was referring to LinleyMike's comment in the Reddit link. Using the Start-Job command so the rest of your code doesn't get caught up in a loop or timer. – Sorean Apr 19 '20 at 22:25

0 Answers0