The aim of the PowerShell console for Sitecore is to create a command line interface to your data so you can automate and aggregate mundane tasks as well as create statistics and a discoverability layer on top of your content.

Have you ever found yourself:

  • having to make a mundane change to a large number of pages?
  • in need of getting statistics field or template usages?
  • being curious of e.g. what’s the oldest page on your site?
  • in need to find out how many authors really create your content and how active they are?
  • having to copy or move a large number of files from one folder to another?
  • renaming or deleting files in your file store en-masse?
  • publish pages that match some specific feature rather than a whole branch?

sharp_toolIf the answer to any of those (and more) is something akin to “yes I did!”, I believe you might find my little plugin useful.

The idea is to create a scripting environment to work within Sitecore process, being able to make native calls to Sitecore API and modify content on a per-property level. The goal was to make it familiar to your IT and developers so they can reuse their skillset with it or if they rather learn it here, this knowledge will benefit them in the long run as clearly PowerShell is becoming an industry standard. The plugin’s aim is to manipulate not just sites, but files and pages on a large scale or perform statistical analysis of your content using  a familiar and well documented query language. The console allows you to execute and test scripts interactively, but also gives the admin means of exposing scripts to end users within context menus and in the ribbon. I’m sure in the long run I’ll come up with more applications. Scripted pipeline processors? Scripted renderings? Scripting campaigns statistics and engagement workflow steps? You name it…

Now there is Rocks interface that performs similar task which I think is brilliant, however slightly – yet significantly different. Rocks integrates with your Windows PowerShell console and allows you to work on your remote website as if it was a file system on your machine. Rocks also features a number of commandlets that help you with some common tasks. The definite bonus of this approach is that you can simply use it in your deployment script and integrate some common content modifications, or clear caches/recycle bins on your server in a timely manner from your monitoring environment. I think the solution is a really needed addition to your Sitecore toolbox, but… it’s somewhat limited. Namely whatever new idea you want to employ in your script, is either supported by the commandlets provided – or impossible. You have access to all the goodies of rocks, but any extension must first be imagined and implemented in Rocks (both server and client side).

The Sitecore Console however works within the process of your Sitecore site and therefore can instantiate and execute any API Sitecore features. The only limit is that of the imagination. this comes at a price too though. The most important thing is security. The PowerShell plugin by itself does not run with elevated privileges, nor does it circumvent the rights, but since you can use any Sitecore API a malicious person could potentially do it themselves – therefore you need to be careful whom do you grant access to the PowerShell Console.

The console comes with some sample scripts when you install it – review the script and press Ctrl+Enter to execute and see some of the interesting use cases for it.

By default the console creates a “drive” for each of your databases so you will definitely have a “core” and “master” drives, and you will most probably have a “web” drive.

Since this is only an introductory post I’ll let you discover the full list of commands by yourself, just use the following script within the console:

get-command `
  | where-object {$_.Implementingtype.FullName -match "Cognifide.PowerShell"} `
  |% {$_.Definition}

Cool, isn’t it?!

The best thing about it is that you can pipeline the commands so you can make a somewhat sophisticated query for items and publish them in a single line. For example to publish all items under Home that are created by admin it’s sufficient to use:

get-childitem master:\content\home\* `
  | where-object {$_.CreatedBy -eq "sitecore\admin"} `
  | publish-item -verbose

To publish all of items I (the current user) have modified you would use:

get-childitem master:\content\home\* `
  | where-object {$_.UpdatedBy –eq $me} `
  | publish-item -verbose

So who edited those pages over the last 7 days?

get-childitem master:\content\home\* `
  | where-object {[datetime]::Now.Subtract([Sitecore.DateUtil]::IsoDateToDateTime($_.Updated)).Days -lt 7} `
  | select-object { $_.Name, $_.UpdatedBy, [Sitecore.DateUtil]::FormatIsoDate($_.Updated) }

You get the idea…

I’ll try to provide more content regarding the Console and its usage over the coming weeks, stay tuned for much more fun!

[Download & Enjoy]

All feedback appreciated!

Disclaimer – Responsibility: With great powers comes great responsibility – this tool can do a lot of good but also bring harm to a lot of content in a very short time – backup your system before using the plugin and never run any untested scripts in a production environment! I will not be held responsible for any use of it. Test your scripts on your development server first! Test on an exact copy of production before running scripts on production content.

Disclaimer – Security: If you plan to use the tool on your production environment, make sure to set the rights up in a way that an irresponsible users or just those who do’n’t necessarily need to, have no access to it. You can additionally, protect it with a <location> entry in web.config, to add another layer of security especially if your editor UI is available on a public server. Manage your group memberships & rights responsibly!

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



This entry (Permalink) was posted on Thursday, November 17th, 2011 at 8:14 pm and is filed under .Net Framework, ASP.NET, C#, PowerShell, Sitecore, Software Development, Solution, Web applications. 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.