In this article series:
- Feature upgrade (part 1) – fundamentals
- Feature upgrade (part 2) – a sample to play with
- Feature upgrade (part 3) – introducing SPFeatureUpgrade kit
- Feature upgrade (part 4) – advanced scenarios
- Feature upgrade (part 5) – using PowerShell to upgrade Features (this article)
OK, so I lied in article 4 about this series being finished – I’ve now added PowerShell support to my SPFeatureUpgrade kit, a set of tools designed to help manage the process of upgrading Features in SharePoint 2010. In this article I’ll talk about the PowerShell cmdlet I’m providing, but also some interesting things I learnt about PowerShell along the way – turns out writing a custom cmdlet is a great way to learn some aspects of PowerShell! These might be of interest if, like me, you haven’t done tons of PS to date.
There are several reasons why you might prefer to use PowerShell to upgrade Features, rather than the administration pages also in the kit:
- You have a large farm, and upgrading hundreds of site/web-scoped Features could time out in a web process
- You’re scripting lots of other things
- You want to use some advanced logic to upgrade some Feature instances and not others
- You don’t want to write a C# console app (or similar) to do this, because that would be well, “a bit 2003” :)
As I mentioned in the earlier posts, SharePoint 2010 does not provide any handy cmdlets (or UI) out-of-the-box for upgrading Features, so this is the gap I’m hoping to fill somewhat.
How to upgrade using PowerShell
Install the supplied WSP to get the admin pages (see earlier articles) and the PowerShell cmdlet. Once installed, the command is:
Upgrade-SPFeatures -Scope <SPFeatureScope> [-WebApplication [<SPWebApplicationPipeBind>]] [-Site [<SPSitePipeBind>]] [<CommonParameters>]
Let’s consider some scenarios – the following table shows the command to use to depending on the scope of Features you are upgrading:
|Farm||Upgrade-SPFeatures –Scope Farm||All farm-scoped Features requiring upgrade are upgraded.|
|Web application||Upgrade-SPFeatures –Scope WebApplication||All web application-scoped Features requiring upgrade are upgraded.|
Upgrade-SPFeatures –Scope Site
|All site-scoped Features requiring upgrade within the specified web application are upgraded.|
|Web||Upgrade-SPFeatures –Scope Web –Site “http://cob.collab.dev/sites/site1” |
---------------------------------- OR -----------------------------------Get-SPSite “http://cob.collab.dev/sites/site1” | Upgrade-SPFeatures –Scope Web
|All web-scoped Features requiring upgrade within the specified site collection are upgraded.|
When the command is executed, if any Feature instances fail to upgrade you will see in the result:
In terms of the breakdown of commands/parameters, effectively this is the same model my admin pages use – farm and web app Features are upgraded without specifying a filter, but for site or web Features you must specify the parent object. As you can see from the alternate usages for these scopes, the –WebApplication and –Site and parameters can be specified inline or piped-in. Being able to pipe in is useful as it allows you to filter the parent object in order to, say, only upgrade the Features in certain web applications like so:
Thus this gives you more control over the parent object and therefore what gets upgraded. But what happens if what you’re doing just doesn’t match how my commands are broken down?
Using other logic to selectively upgrade Features
Examples of this might be “upgrade all web-scoped Features where the web is in this list [‘foo’, ‘bar’]”, or perhaps “upgrade all site-scoped Features where the site has other Feature ‘foo’ activated” and so on. For now, my cmdlet won’t really help you too much in this case so you will need to write some PowerShell which goes straight to the API. For upgrading web-scoped Features, this might look something like:
Obviously this example would need amending for other scopes, proper error reporting etc. Clearly, unless you’re very comfortable cutting PowerShell, having a cmdlet which contains most of the logic does make things easier. So at some point I’ll extend my cmdlet to somehow allow you to filter on which Feature instances to upgrade rather than upgrading all of them - perhaps by specifying a list in an XML file or similar. I could easily get PowerShell to ask you to confirm for each item in the loop, but obviously that doesn’t scale when large numbers of Feature instances (e.g. web-scoped Features) are involved and is no good for unattended use, so that doesn’t feel like the way to go.
Using –WhatIf to see what *would* be upgraded
My cmdlet fully supports the PowerShell –WhatIf switch – this can be very useful as it allows you to see what Feature instances would be upgraded without actually doing anything. In the image below I can see all of the webs in my site collection where my Feature would be upgraded:
There are a couple of shortcomings which spring to mind in the current implementation. Actually an experienced PowerShell eye would probably find lots (feedback welcome, help me learn!), but I do know of these:
- No ability to filter which Feature instances are upgraded (as discussed earlier)
- Doesn’t return an appropriate object to the pipeline for downstream processing
- Actually I only gained a real understanding of this after I’d uploaded to Codeplex. I’m happy to leave it for now, but will improve this in the next version.
The cmdlet and the admin pages I blogged about previously can be downloaded from http://SPFeatureUpgrade.codeplex.com. Here’s what my current code for the cmdlet looks like:
Appendix - some interesting things I learnt about PowerShell
- The pipeline concept – much like piping elsewhere (UNIX, jQuery spring to mind).
- This basically allows multiple commands to be chained together into very powerful one-liners. Made possible by the fact that each cmdlet returns an object to the pipeline for the parent script or downstream cmdlets to process.
- Passing parameters to PS functions – the syntax doesn’t separate parameters with a comma, which no doubt always fools newbies!
- So an example from a script I wrote yesterday:
SetPropertyOnWebApp $webApplication "portalsuperuseraccount" $objectCachePortalSuperUser
- The SharePoint PowerShell framework comes with custom ‘PipeBind’ objects for many SharePoint objects – using these for your parameters allows the SharePoint object to be identified in different ways (e.g. SPSite by URL or GUID)
- Implementing Should-Process/-WhatIf in a cmdlet – thanks to Shay Levy (PowerShell MVP) for helping me with this :)
- If your cmdlet has a property of ‘SupportsShouldProcess=true’ on the Cmdlet attribute, callers can use the –WhatIf switch. You can test for this by calling the ShouldProcess method on the base Cmdlet class – it will be false if –WhatIf was used.
- A related approach is used when your cmdlet should ask for confirmation (-Confirm) before making a change/writing data
- Parameter sets in a cmdlet
- So long as the caller specifies at least one unique optional parameter, you can identify which ‘set’ of parameters was used and do conditional logic on this. This is akin to method overloading in C#.
A great source of learning for SharePoint/PowerShell development is of course, Gary Lapointe – the source code for all his cmdlets is published on his blog.