Execute Sitecore Powershell from Sitecron Schedule Job

Yes you heard it right.  I was planning on writing a couple of sample jobs to do indexing etc but then I realized why not make it a lot more powerful.  So I decided to add support to run Powershell scripts.

As I mentioned earlier you can implement the IJob interface and implement what ever you choose to.  I am including a default Powershell execute job which can take in the parameters you would like to pass to your script and specify a link to the Powershell Script item.

This gives you a lot of power in terms of the kind of functionality you can build. Better yet, you can tweak this functionality and re-run it without recompiling the code.

First I would like to go over the fields and what you can do with them:

powershell_sitecron

  1. Type: class namespace, assembly (e.g. Sitecron.Powershell.ExecuteScript, Sitecron)
  2. Cron Expression: 0 15 10 * * ? (e.g. Fire at 10:15am every day)
  3. Parameters: test=1&p=first (e.g. you can pass in anything like a guid/some value or values similar to the Sitecore parameters so that you can get a NameValueCollection. It all depends on your need.)
  4. Items: Select one or more items you need to do the job. For instance if you need specific items to do import/rearrange items/rename etc.

I tried to make this generic so that based on your needs you can implement the IJob Quartz.NET interface and achieve the functionality you need.

From my testing the scheduled tasks always executed on time every time.

The other thing to note is that as part of the output, you can output the execution time and next scheduled execution time.

Now for the Powershell script. Here is the code I implemented so that its part of Sitecron and you can utilize it or implement your own. Needless to say that in order for this Powershell job to run you would need to install Sitecore PowerShell module 3.1+

Adam Najmanowicz was nice enough to take time to suggest ways to implement it.

One thing to note here is that although we have the ability to select multiple items, in this case multiple Powershell Scripts, it is not a good practice to execute multiple scripts via code consecutively.  There could be inter-dependencies, one of those scripts could be calling another script(s) etc. creating issues where there shouldn’t be one.

The intent is to select one item of type “PowerShell Script”.  If multiple are selected this specific implementation will only pick the first one and ignore the others.  I have included commented code which is capable of executing multiple script items but use it at your own risk.

I am posting the update to the module to the Sitecore Marketplace shortly. I updated GitHub.

GitHub: https://github.com/akshaysura/Sitecron
Market Place: https://marketplace.sitecore.net/Modules/S/Sitecron
For help with Cron Expressions you can use http://www.cronmaker.com/

If you have any questions or comments, please get in touch with me.

 

no comment

Add your comment

Your email address will not be published.