Kyle Kingsbury A Sitecore web development blog!

Creating a custom command with Sitecore PowerShell Extensions

C

Ever wondered how to extend Sitecore PowerShell Extensions (SPE) and add custom commands to streamline your development? In this post, i’ll review what you need to do to quickly start development.

The Sitecore PowerShell Extensions (SPE) module is a Sitecore development accelerator which can drastically increase your productivity and curtail the amount of time it takes to deliver a Sitecore solution.

Adding the binaries to your project

If you haven’t already installed SPE, head over to the installation guide and download and install the module into your solution.

Unfortunately, the SPE project only provides the releases in the form of Sitecore Packages and not provided as Nuget packages; not a blocker, but not convenient either. Within your Sitecore instance, navigate to your bin directory and copy the Cognifide.PowerShell.dll binary into a directory of your repository. For example, for libraries/binaries that are not found on Nuget, I will copy these files to a libs folder at the root of my repository. Within Visual Studio, go ahead and add a reference to this binary and set the property Copy Local to false.

Next, using the Nuget Package Manager, we will need to add a reference to System.Management.Automation.dll, version 10.0.10586.0 to your project – again – setting the property Copy Local to false.

Creating a custom PowerShell command

Ok, so on to the fun part of the post, writing the custom PowerShell command.

As an example, lets say I want to make it easier to find products by Sku using the ContentSearch API. I would create a new class that extends the Cognifide.PowerShell.Commandlets.BaseCommand class.

Example Command

A few things about this class:

  • Adding the CmdletAttribute to the class, provides the ability to expose the command as by a unique name to the PowerShell console. It is recommend to use, the Verb-Object modifier.
  • Adding the ParameterAttribute to a public property, we provide the ability to pass parameters from the console to the custom command. Some properties may need to be mandatory, you can force the console to error out if the property was not provided by setting Mandatory = true
  • A custom command has a few methods you can use to execute your custom code: BeginProcessing, EndProcessing, and ProcessRecord
    • BeginProcessing – Performs any initialization of a command. If not overridden, the base implementation simply returns
    • EndProcessing – Performs cleanup or post steps for the command. If not overridden, the base implementation simply returns
    • ProcessRecord – Called for each input record that is processed by the cmdlet
    • For more information, review the Cmdlet Input Processing Methods from Microsoft
  • Using the WriteObject method, we can write data back to the console. This is essentially the output of the command

Registering the custom command to Sitecore PowerShell Extensions

Now that we have our custom command class written, we need to expose this command to Sitecore PowerShell Extensions. To do this, we need to add additional configuration:

Using the custom command

After all of our hard-work, we finally get to use our custom command.

And thats it, simple and straightforward! I hope you enjoyed this post and as always, be sure to leave a comment!

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By Kyle Kingsbury
Kyle Kingsbury A Sitecore web development blog!

Recent Posts

Tags