SharePoint and PowerShell Demo – Part 1

I am not surprised at the number of well-known people in the SharePoint community who confessed they have heard of PowerShell (or Monad) but have only played with it. As far as I know there was not a big PowerShell product launch. MOSS and WSS 3.0 did not release with any PowerShell commands and therefore did not beak into the SharePoint realm.

There are many places in the PowerShell community where someone interested in learning or understanding PowerShell basics can go and see the standard Get-Process demo. Because this is a SharePoint blog and not a PowerShell blog I am going to jump right into a PowerShell / SharePoint demo and skip the standard Get-Process demo that appears everywhere in the community. This demo will use a custom cmdlet (pronounced command-let) with the built-in functionality of PowerShell. This post will not discuss the creation of the custom cmdlet It is only provided to demonstrate some of the key PowerShell features. A later post will discuss the creation of the cmdlet.

Scenario
As an administrator or developer looking under the covers of MOSS or SharePoint I always come across feature ids (GUIDs) . The only way to determine what feature is associated with the feature id is to look through all the feature folders located in the TemplateFeature directory, inspecting each feature.xml for the matching feature id. Initially I created a custom Stsadm.exe command called GetFeatureDefinition (which I will describe in a later post) that accepted a GUID Feature Id as an argument and returned the associated feature information.

Part 1 – Executing a Simple Cmdlet
It turns out that this same functionality, GetFeatureDefinition, becomes even more powerful when exposed as a PowerShell cmdlet. PowerShell cmdlet syntax is verb-noun. These are standardized with a limited set of verbs such as Get, Set, ForEach and Select. Nouns can generally be any noun which the verb is acting upon. Commonly syntax in PowerShell would include ForEach-Object, Select-Object and New-Object. The verb-noun command for the demo custom cmdlet is Select-FeatureDefinition. Select-FeatureDefinition accepts one parameter FeatureId which is string representation of the Feature Id (GUID). The figure below shows the PowerShell editor ready to execute the Get-FeatureDefinition cmdlet with a feature id of “00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5”

With a click of the Enter key PowerShell returns with:

The cmdlet and PowerShell found the associated Feature Definition and output a text representation to the screen. I now can can tell that I am looking at the Feature Id for the Team Collaboration Feaure.

Part 2 -Using the Pipeline
If you are a developer or an admin who knows something about development you can probably see the similarity between cmdlets and functions. One thing not obvious in this example is that the Feature Id argument can be passed in as a parameter or can be passed in from the pipeline which is where you start seeing the power of PowerShell.

The pipeline is simple, create a chain of PowerShell cmdlets and functions where the output of one cmdlet or function becomes the input of the next using the pipe “|” character. The next example passes in the same Feature Id to the Select-FeatureDefinition cmdlet and returns the text representation the Feature Definition.

P

This time the string is placed before the pipe character. Effectively PowerShell will use the pipeline to send the string to the Select-FeatureDefininition where it will be used as though we defined it as a parameter. The pipeline is a real powerful feature. Look at the next image where I pass in two Feature Ids to the cmdlet (comma-separated).

And now the results:

Both Feature Ids were processed and their associated Feature Definition displayed as text. The pipeline passed each Feature Id to the Select-FeatureDefinition cmdlet. Effectively any function or cmdlet that outputs a Feature Id can be used as the input of the Select-FeatureDefinition cmdlet.

The piple is a powerfull tool that allows for composition. Developers and administrators create and work with smaller chunks of defined functionality and can use the pipeline to chain the functionality togheter to build larger functionality. That is enough for now. The next PowerShell/SharePoint post will be part 2 where we will continue to build out our example using Select-FeatureDefinition.

3 Comments

  1. John W Powell 11/12/2008
  2. Novotronix Blog 11/12/2008