Things you might not know unless you Automate your first task using task scheduler

If you scheduled some script execution using task scheduler and its running fine only when you are logged in the system and failing when you are not logged in than keep reading !

Few months back I got moved to a new team to take on new challenges within my company where I had to pull some daily reports as a part of my new role apart from technical work.

The process of pulling daily reports included logging into a SSL gateway,RDP a client domain server/jump server run few batch scripts there which in turn would produce few excel report files compressed within a single .rar file as output, ready to be copied to my local desktop, simple enough.

As the batch scripts were to be executed at a particular time I chose windows task scheduler to do this work automatically for me instead of doing it manually as it was perfect fit for my need and moreover I dint had to write a single line of code or any other new script to serve my purpose,scheduling a task with task scheduler is easy, few clicks here and there and you are done right? well at least that's what I thought before I ran into problems that I never knew even existed.

For testing the feasibility of concept I opened windows task scheduler and created few basic tasks to execute a program(batch scripts) daily at the specified time.

During the execution of the scheduled job I noticed an unwanted behavior of the batch scripts they were running properly and the scheduled task was also getting completed successfully but there was no output file getting generated as expected.

The reason was those batch scripts were calling VB scripts at the end to open an instance of Excel, read some data from various text files and write it into the excel cells after adding several sheets to the workbook.

So let me come to the point,there are 5 main changes in total that are suppose to be made if you are willing to run your scripts using task scheduler when even no user is logged in without any user interaction at all :

  • You need to remove everything which needs user interactivity for example in my case it was a simple message box with a OK button which used to pop up at the end of successful execution of all batch and vb scripts.For filling the gap i added a line of code to the script to write the success/failure of execution of scripts in a text/log file instead of showing pop up messages.
    msgbox "OK" --Remove any such line
  • You need to set the visibility of every instance of Excel in your script to false
    objExcel.Visible = false
    Trust me excel can create more problems then you might think.If your script is fine and still expected output is not coming make sure the excel instance is not opening in protected mode,to prevent this problem just open the excel manually and click on enable editing,the other best thing to do to solve this problem permanently is to add the location of your template excel file in trusted file location from File>>>Options>>Trust Center. so that protected mode problem does not happen at the first place.
  • You will be required to create a empty folder named "Desktop" in the following directory paths:
    C:\Windows\System32\config\systemprofile\Desktop
    C:\Windows\System64\config\systemprofile\Desktop
Note:The above step resolves the problem of scheduled tasks not able to run batch and vb scripts when no user is logged in even though the scripts are running fine when a user is logged in.
  • While creating the task you need to set the "job starts in" argument with the path to your batch/vb script that you are trying to run using task scheduler.
  • You need to prefix complete path to your VB Script with the following line if its not being run directly and is being called from a batch script instead :
  • csscript C:\System32\cmd Path to your VBSCRIPTS here
    

Comments

Popular posts from this blog

Adding Home Older Newer Post Buttons in new Blogger templates such as Contempo

Adding copy to clipboard button to every google code prettify pre blocks in blogger

Checklist: Before applying for adsense