This weekend was the Belgium SharePoint Saturday, held in Brussels. I had the pleasure of being able to present a session of PowerShell and scripting environments. I have to say, the whole day was excellent and the team at BIWUG did a great job of organising it. In total there was 6 tracks with 6 session so with 36 or so sessions running through-out the day there was a lot of things going on. Everything pulled together and the feedback so far has been great. I got to meet some new people, catch-up with people I’ve met before and meet people first time face to face!
PowerShell your SharePoint environments – from setup to finish
My aim wasn’t to provide a complete break down of all the scripts required to build an environment, but rather demonstrate just how far PowerShell could be taken. The demo aspect was to show, how through scripting, we could take a basic server and configure the farm, setup a web application and site collection, deploying some custom code and finishing with deploying some content as well. From blank vm to fully working site without touching any UI – at least that was the plan!
Having tested the scripts the day before I felt confident the demo would run smoothly – the biggest challenge being time. Alas the powers that be that watch over demos were clearly not on my side as my vm developed some odd SQL/network related connections issues meaning I couldn’t run the scripts! (Turns out my snapshot failed on me). Fortunately I still had a backup vm from testing so I could revert to that, even if I had to rely on talking more through the process rather than actually seeing the scripts run. Overall the session didn’t run quite as seamless as planned but we did get there in the end and the audience was very understanding. With my reverted backup I was able to show a new vm and the final site, its just the audience had to take my word that the scripts were used!
So despite the hiccup, what did I cover?
The main point of the demo was to see how PowerShell scripts could be used to completely deploy an environment. I’ve included the scripts and code in a zip file, which you can download and refer to (see end of this post).
Script overview
I like to package everything, especially my PowerShell scripts, into a easier manage format. So rather than having lots of different scripts which need calling, I prefer to have a single ‘deploy-me’ script which can be called and passed different arguments. For example, I would call ‘deploy-me.ps1 farmsetup’ to trigger the script to setup the farm, ‘deploy-me.ps1 webapp’ to trigger the script which creates the Web App and Site Collection. Whilst this approach means the scripts are a little more complex (you need to allow for the arguments to be passed in and switch statement), I’ve found it does simplify the deployment process as only one script ever needs to be directly called in the console.
In the demo I put together for this session, I created the following scripts -
- deploy-me.ps1 – this should be the only script that gets called at the console and is provided with different arguments
- Create-Farm.ps1 – this script creates the Farm and installs Central Admin (same as running the Product Configuration Wizard)
- Create-WebApp.ps1 – this sets up the Web App and Site Collection
- Deploy-Solutions.ps1 – this script installs/add/upgrades/activates/deactivates the feature
- Create-Content.ps1 – this script creates some content and as an example loops through the ‘thesharepointbaker.xml’ file for data
- CreateList-CommonLinks.ps1 – this scripts creates a custom list and populates with some data
- \EnvVars – this folder contains an example set of environmental variables
- \Scripts – this folder contains some of the custom PowerShell functions
As I said in the session, you may disagree with the approach I’ve taken and by all means do. This is one approach I’ve seen work for projects I’ve worked on, but for your particular setup you may prefer to do something different. I know that a lot of developers for example would prefer to create the new List through a Feature receiver instead of PowerShell.
I’m planning a series of posts aimed at PowerShell for developers and starting to look at how we can use it and do a lot of the very bespoke stuff.
Downloads
You can download a zip of the scripts and WSP here > SPSBE PowerShell Scripts
You can also checkout the slide deck on http://www.slideshare.net/sharepointbaker/spsbe-2012-powershell-and-sharepoint
(Feel free to reference these scripts and code, but note they are not necessarily Production ready – they were written for this demo!)
