I have a few powershell scripts that are scheduled, and they usually run just fine (both as a scheduled task, and when run manually)

I've got an issue where occasionally, they just stop working midway through a script.

One of my scripts looks like this:

write-host "Starting!"
foreach($blah in $blahblah)
   write-host "    $blah"
write-host "Finished"

Whats odd is that when I look at the transcript I see this pattern:

Windows PowerShell transcript start
Start time: 20170116055439
Username  : domain\user 
Machine   : PCNAME (Microsoft Windows NT 6.1.7601 Service Pack 1) 
Transcript started, output file is GetMSOLLicensestoCSVTranscript.txt
    blahitem 37675
Windows PowerShell transcript end
End time: 20170116060001

Note that it just stopped in the middle of the loop (it wasn't done with all the data) and note that the write-host "Finished" never got called.

What I found interesting was that powershell still somehow closed up the transcript file and wrote the last 4 lines.

I semi assume it's running out of memory, but don't know how to tell if that's really what is happening.

Any troubleshooting tips would be appreciated.


Is the code within the loop pure powershell or calling someCrazy.exe? Some crazy exe's write to console instead of standard output, and that kind of error would not be captured by transcript. You could try wrapping the powershell with CMD which may capture that kind of error text.

CMD.EXE /C "powershell -file c:\your\script.ps1" 2>&1 > c:\temp\cmdWrapper.log

Re: missing "finished". Stop-transcript is one way to end a transcript. Likely that the error within the loop is causing powershell to exit. The transcript will will stop with the normal close message when powershell.exe spawned by task scheduler ends.

  • Hey Craig! I found the issue and it was a stupid root cause (ie my own fault) I'll post it below in case it helps someone else. – basementjack Jan 18 '17 at 18:27

Ok so the (embarrassing) answer was this was a mistake on my part.

In windows task scheduler, there are multiple places you can set a time limit - One overall which is on the main set of tabs, but then you can also set a time limit for each task you specify.

When I setup the scheduled task to run the script daily, I set it to run for a max of 1 hour.

So while the overall combination of all tasks could take up to 4 hours, the specific subtask that launched powershell was limited to an hour.

I missed this when I checked and saw the 4 hour setting.

  • Jack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.