Why Doesn’t My PowerShell Script Exit Properly After Running a Task?

0
2
Asked By CuriousCat123 On

I'm running a scheduled task on Windows that executes a PowerShell script (script1.ps1). This script sets some variables and calls another script (script2.ps1). After script2 finishes, it renames a log file and creates a new one. Normally, the whole process takes a couple of minutes, and the task is set to stop forcefully if it runs longer than 60 minutes. Lately, script2 seems to finish, but the scheduled task doesn't return control to script1, leading Windows to force abort it after the timeout. I've checked the logs from script2 and they're showing it completes with an exit code, but it seems like control isn't returning as expected. I even experimented by adding an additional exit command after the exit code, but it hasn't resolved the issue. Also, when I run script2 directly from a PowerShell shell, I experience a similar hang, where it fails to return to the prompt. What could be causing this issue?

2 Answers

Answered By TechieTed88 On

How are you determining that script2 has finished? Are you just checking the log line, or is the child process still active? It sounds like the child PowerShell process may still be running, which means script1 will keep waiting for it to finish. Sharing script2 could help clarify things.

CuriousCat123 -

The log line indicates that script2 is complete, and the next action is the 'Exit' command.

Answered By PowershellNinja42 On

It sounds like script2 might be hanging. If it's not returning to script1 even when you run it independently, the issue probably lies within script2 itself. Can you share the contents of script2 for us to help troubleshoot further?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.