As with any computer program, you need to have error handling and logging in your code so that when something bad happens, you will know what statement failed, and what error code you got. That is especially important for an unattended processes like a scheduled task where there is no interactive user who could see the command window and the script output.
Start by adding a transcript to the script.
Add try/catch blocks and display key variables (processing steps) so that you know what the script is doing, and you don't have to guess.