Sitecore PowerShell Extensions History engine – Improved!

Yesterday I had written a blog post on Sitecore PowerShell Extensions History engine? As it was pointed out in the blog post and by Mike Reynolds & Kamruz Jaman (who refuse to be my friends). We have to improve the code by adding a pipeline. It bothered me enough all night, that I had to get this out of the way. One its a bit difficult as we are dealing with Windows PowerShell at its code. So post script execution, I decided to add a new pipeline which gets executed along with the commands and the errors reported.

Anyone can tap into this pipeline and add additional processors. Again, I hope this gets integrated into SPE some how, or Adam/Mikey will make this better.

Here goes. First lets setup to have a brand new pipeline but defining the PipelineArgs, here is the sample code:

Once that is done, lets define the interface the pipeline processor will be based on, here is the sample code:

Next, lets define the processor it self. This processor’s job is to log the commands into the MongoDb connection we made using the previous blog post. Here we are passing in the MongoDB connection string by config.

Here is the sample code:

Finally in the Cognifide.PowerShell.Core.Host.ScriptSession class, we will tap directly into the ExecuteCommand method. We are going to add some code right after the command is executed around line 740. We are going to load the Args with the commands and the errors if we see the HadErrors is set true. One thing to notice is, that depending on the command, we might see more than 1 in the commands collection. Here is the sample code:

Once that is all set, we are ready to create the new config to load the pipeline and the processor. Here is the sample config:

Rebuild and run a few commands as two different users. As you can see from the screenshot, it records them in Mongo.


If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

no comment

Add your comment

Your email address will not be published.