Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Archive for the ‘SharePoint Tools’ Category

Preparing PowerShell for SharePoint and MOSS 2007

with 18 comments

Microsoft PowerShell allows you to write scripts that can use .NET classes, objects, properties and methods. It therefore provides an ideal way of creating script files by calling into the SharePoint and MOSS object model.

First, download Microsoft PowerShell from here and install. PowerShell can then be lanched from the “Start” menu.

The PowerShell execution policy determines if script files can be run – by default they are prohibited for security reasons. Enter the following command to find out about execution policy and signing script files:

get-help about_signing

You can either elect to only run signed scripts (in which case scripts must be signed with a certificate), or to run unsigned scripts from the local file system or to run any unsigned script. For most users, allowing unsigned scripts to run from the local file system is acceptable, so to allow this enter the following commands to set and then retrieve the execution policy (you need to be logged on as administrator):

set-executionpolicy RemoteSigned
get-executionpolicy

Before programming against the SharePoint and/or MOSS 2007 object model the appropriate assemblies must be loaded. The following commands do this:

[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

You can place these commands the file called Microsoft.PowerShell_profile.ps1 in a folder called My Documents\WindowsPowerShell to run commands automatically when you run PowerShell (the folder and the file may not exist and may need to be created).

You can now test calling into the SharePoint object model. The following command gets a reference to a site collection at the given URL:

$spsite=[Microsoft.SharePoint.SPSite](“http://localhost/sites/intranet“)

You can review all the property values for the SPSite object by typing the name of the object variable:

$spsite

You can find an example of creating a script file for chaning a site’s regional settings here.

 

 

 

 

 

Advertisements

Written by Nick Grattan

September 3, 2007 at 11:19 am