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.

Technorati Tags: ,,

Advertisements