So now that you can easily create the menus and ribbons executing PowerShell scripts, it might be a good time to do some more administrative tasks… let’s start with scheduled tasks. I’m not going to go deep into the task scheduling, you can find an excellent post by John West on his blog regarding task scheduling. Currently the PowerShell module supports one of the ways described in the post – the section describing how to configure them is called “Scheduled Tasks” in the blog.

The module comes with an out-of-the-box PowerShell script command as you can see

PowerShellScheduledTaskCommand

You don’t need to create any additional commands, instead you only need to create a script and a schedule for it and the existing command will handle all of them. In my example I wanted to trim all recycle bin items every night.

Let’s get cracking…

First you need a script that will accomplish the task. The module automatically creates a Scripts folder in the following location: /sitecore/system/Modules/Scripts/. It probably is best if you stick to putting your scripts there. The Insert options for it are limited to scripts and folders so you can organize your scripts into subfolders if the number will grow significantly.

In my case the script part will be fairly simple:

EmptyRecycleBinScript

The Popup window and Pre-execution warning settings in this case have no meaning as the script has no means of providing this kind of feedback as it’s executed in a background thread.

Now create a schedule for your script in /sitecore/system/Tasks/Schedules/:

EmptyRecycleBinSchedule

I strongly encourage that you read more about how task scheduling works in Johns blog. An additional feature you might find useful since there is no UI component to running scheduled tasks is the new Write-Log commandlet, which wors similarly to Write-Host but instead writes to Sitecore diagnostic logs. The syntax is as follows

Write-Log [[-Object] <Object>] [-Separator <Object>] 
  [-Log <LogNotificationLevel>]

So for example to write an info message you can use:

Write-Log “Recycle bin clear scheduled task started”

Similarly you can write a Debug message like:

Write-Log –Log Debug “This is a debug message”

This supports all of the common log types like: Debug, Error, Fatal, Warning & Info – if no log type specified – Info will be used.

The solution requires the latest Sitecore PowerShell Console (1.2.0) from Cognifide, available for free from Sitecore Shared Source site.

All feedback appreciated!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Tuesday, November 29th, 2011 at 5:21 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.



  • Sandip Patel

     Hi There,
    I have got following error any idea why am i getting this

     ERROR Error executing command item: PowerShellScriptCommand
    Exception: System.Reflection.TargetInvocationException
    Message: Exception has been thrown by the target of an invocation.
    Source: mscorlib
       at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at Sitecore.Tasks.CommandItem.Execute(Item[] items, ScheduleItem schedule)

    Nested Exception

    Exception: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    Source: Cognifide.PowerShell
       at Cognifide.PowerShell.Shell.Host.ScriptSession.Initialize()
       at Cognifide.PowerShell.Shell.Host.ScriptSession.ExecuteScriptPart(String script, Boolean internalScript)
       at Cognifide.PowerShell.Shell.Host.ScriptSession.ExecuteScriptPart(String script)
       at Cognifide.PowerShell.Tasks.ExecuteScriptTask.Update(Item[] items, CommandItem command, ScheduleItem schedule)

    ManagedPoolThread #13 17:14:16 ERROR Error executing schedule item: EmptyRecycleBinSchedule
    Exception: System.Reflection.TargetInvocationException
    Message: Exception has been thrown by the target of an invocation.
    Source: mscorlib
       at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at Sitecore.Tasks.CommandItem.Execute(Item[] items, ScheduleItem schedule)
       at Sitecore.Tasks.ScheduleItem.Execute()

    Nested Exception

    Exception: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    Source: Cognifide.PowerShell
       at Cognifide.PowerShell.Shell.Host.ScriptSession.Initialize()
       at Cognifide.PowerShell.Shell.Host.ScriptSession.ExecuteScriptPart(String script, Boolean internalScript)
       at Cognifide.PowerShell.Shell.Host.ScriptSession.ExecuteScriptPart(String script)
       at Cognifide.PowerShell.Tasks.ExecuteScriptTask.Update(Item[] items, CommandItem command, ScheduleItem schedule)