A place for Sharepoint and rantings
WSS and PowerShell
The more I do with SharePoint the more and more I an finding a use for PowerShell. I am using WSS as an application platform and using lists etc as a datasource. Providing what you want is not huge amounts of referential integrity and transactional control etc it works pretty well.
But when things go wrong, often the only way to quickly get in a fix something in a structured, repeatable and documented manner is PowerShell. Hell and its fun too.
So as a Sharepointy person what do you need to learn to get you quickly started.
First go and install PowerShell, this is version 1 which is what I am using, version 2 will be great but is not in production yet, so try getting that on your live servers, if they let you, sack them – pre-release code on production servers pah!.
Next go and download Quest’s PowerGUI. This comes with the fantastic script editor, that allows stepping through code and debugging, i tend to use this for all my PowerShell development, I’m used to Visual Studio so being confronted with a command shell environment can be a little daunting.
Now if you go looking on the web at powershell there are some great resources and experts, I am not one of them I barely know PowerShell at all, I just rely on some simple things to get it working with SharePoint.
The biggest thing about PowerShell is you can just create .NET objects in it. Yep just go ahead and create an SPSite object and away you go.
So lets dive into an example straight away
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $site = New-Object Microsoft.SharePoint.SPSite "http://localhost/products/MyWSSApp" $web = $site.RootWeb $lists=$web.Lists $list=$lists["MyList"] $item=$list.GetItemById(1) $item["Title"]="Jeff" $item.Update() $item=$null $list=$null $lists=$null $web=$null $site.Dispose() $site=$null
This code will be executed on a machine with WSS installed on it, you can’t remote it from a box without PS and WSS installed. It’s LOCAL.
The first thing we do is load the assembly, without which PS knows nothing of the objects.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
the next thing I’m doing is to create a reference to my site collection
$site = New-Object Microsoft.SharePoint.SPSite "http://localhost/products/MyWSSApp"
The New-Object statement is PowerShell’s way of creating a .NET object, the C# equivalent would be
SPSite site = new SPSite(http://localhost/products/MyWSSApp)
There is no need to declare the type in PowerShell, although it is possible to cast types, which you will need later on in your Scripting, but not today.
All variables in PowerShell are prefixed with a $ sign.
In the next sequence of statement you can see I easily manipulate the .NET Sharepoint object model from within PowerShell. I can grab the RootWeb and assign it to a new PowerSHell variable and likewise with the list and a list Item. Then a simple data change and list update. All things you will be used to doing in C# can be done easily ni PowerShell to.
$web = $site.RootWeb
$lists=$web.Lists
$list=$lists["MyList"]
$item=$list.GetItemById(1)
$item["Title"]="Jeff" $item.Update()
Notice that I still deliberately DISPOSE of the site, this is still important to do even in powerShell.
| Print article | This entry was posted by Binaryjam on July 23, 2009 at 8:50 am, and is filed under Sharepoint, development. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
this seems straightforward and useful, but what is the real advantage over just cranking out a small utility exe (that does the same thing as your code only in c#) that is fired off in a post build event? I am just missing what this can do that a quick and dirty exe cannot.
about 1 year ago
I should add that my comment above was more applicable to the post that changes the assembly versions in the receiver classes. None the less…. just random batch updating of stuff seems to be equally doable in c#… so i guess the same question applies.
about 1 year ago
There is nothing I can do in PowerShell I can’t do in C# in fact i can do so much more in c# and an exe.
But your missing the point or rather I’m not making it very well.
I can knock up a dirty exe and say here sys admin guy, run this dirty exe, no I don’t have an installer, no I haven’t had it tested by an army of users etc…..
Now I give the sys admin guy a pshell script and he says oh you don’t need an installer, oh I can see the code too and review it my self, hmm nothing dangerous there, ok I’ll run it. Then he says, oh yea this would be really handy for me, a tech expert but not a developer, I can automate so many of my daily drudge processes with this, some of them sharepointy.
Sure same bloke could have done this automation in Vbscript and probably did, but now with powershell MS provided a real environment for automated scripted management.
In the example I had in order for me to do the same in an exe all that upgrade code would have had to be built into a custom installer with all the pain that involves, so much easier to have a script that installs the solution upgrade then patches the site as part of a versionised controlled release I delivered to my sys admin peeps.
I also use Pshell to interogate my dev sharepoint, I find this quick and easy to do and much faster than even knocking up a dirty exe, even in snippet compiler.
But you have to make those decisions yourself and determine in your environment is it a good fit, for some it won’t be but for others it will.
about 1 year ago
Hi. About disposing the $site object, you might do even better if you dispose of it right after assigning the $web and on the same line like that: $site=new-object …; $web1=$site.OpenWeb(…); $web2=$site.OpenWeb(); $site.Dispose();
about 1 year ago
In my code I’m not using openweb but rootweb meaning I don’t dispose of the web. So if I dispose the site early what happens ? Honestly I don’t know, yet. The reason I chose to dispose at the end is down to the way I write in c# with a using statement
about 11 months ago
OK, now I finally get Powershell. I work in a shop these days that small and tight knit, so the divide between sys admins and software engineers is very thin. Hence I am not presented with a reticent admin very often. Thanks very much for taking the time to explain it. It was odd to me to see all the developers really excited about pshell until your explanation.