Tuesday, April 1, 2014

Powershell script using excel COM object is running forever through Windows task scheduler.

From past few days I was being haunted by an issue of ‘Windows Task Scheduler’.

Issue: I have a PowerShell script that gets certain SQL server related data from different servers and stores that data in an excel file and sends that excel file to group of people. This script works fine without any issue when I run either through ‘Windows Powershell’ or ‘Command Prompt’. I tried to schedule same script through ‘Windows Task Scheduler’ of Windows Server 2008 R2 server but it never completes successfully.

Either I run the windows task manually or if it starts as per schedule the task goes to ‘Running’ status and hangs in that status forever and never completes. The only thing noticed is whenever this task hangs I can see single or multiple ‘Excel.exe *32’ process in task manager, I tried killing these process but that just lead to failure of the task but script never got executed.

I have been referring to many of the articles but have not found exact reason for this behavior.

Below are the 2 solutions I found so far for this issue: (if anyone else have better explanation and solution for this issue please feel free to share)

1.    You have to create a folder (or two on a 64bit-windows):
(32Bit, always)
C:\Windows\System32\config\systemprofile\Desktop
(64Bit)
C:\Windows\SysWOW64\config\systemprofile\Desktop
This was given by user named ‘Jens Kalski’ in this link and I tried the same thing and my script started working fine. Thanks again Jens Kalsi.

2.    To check the option of Windows task ‘Run only when user is logged on’. Even with this also task will complete successfully but as it says, the task will run only if the user is logged on.

Hope this saves someone’s time!!


Thanks!!

No comments:

Post a Comment