Sitecore PowerShell Extensions History engine?

As many of your know Sitecore PowerShell Extensions is a valuable tool in the Sitecore ecosystem. I cannot recall how many times SPE has saved me from repeated and tedious tasks.

For the longest time I wanted to store the command history for audit purposes. Finally I got some time to get into some code hacking to get this done.

One, you have to realize that SPE relies on Microsoft PowerShell and so we cannot mold it as we want. Ideally it would be great if we could develop a SPE Command pipeline.

So we start off by setting up MongoDB. Part of this blog post depends on work in Use MongoDB to store Sitecore log entries centrally!

Lets start by adding a connection string to a new MongoDB collection:

Setup a class called SPECommand to hold the structure of the record. Here is the sample code:

Create a repository to interact with MongoDB, here is the sample code:

Now that we have everything setup to write to Mongo, lets get into the depths of the SPE code to find the place where we can log the commands being executed.

In the Cognifide.PowerShell project go into Core > Host > ScriptSession.cs file. In the ExecuteScriptPart function on like 599, you can add the following to log the command being executed. Here is the sample code:

Also in the Measure method we could add additional logging on who executed the command for the log entry.

Add in — {Sitecore.Context.GetUserName()} to the LogUtile.Info so that we record the user who executed that command.

This might not be the best way to add the History Engine to SPE but its a start. We could also have additional features like success of the command, script status monitor and a report pulling all of this Mongo information.

spe3

spe2

spe1

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.