Wednesday, October 25, 2006
« Presenting at the SUGUK Meeting 26th Oct... | Main | SharePoint/PowerShell 1: Make me a porta... »

Hi,

I'm about to start off a new series of entries about using Powershell to control SharePoint.  There's a big gap in the ability to script SharePoint between stsadm commands and custom .net programs and PowerShell fills it nicely.

All my entries from now on will focus on SharePoint version 3.

PowerShell is 'DOS for the .NET generation' (c) :-) and its a fantastic way to both interactively control SharePoint via its Object Model from a command line and to create scripts that can be run in a batch. Its currently at version RC2 and is available from here http://support.microsoft.com/kb/925228
It installs as a windows update package rather than a separate install as it used to in earlier betas, thats a sign of its being a core element of Windows going forward.

Ok lets do some Powershell SharePoint basics.

First although Powershell can call any .net class the assembly must be loaded first.
By default Powershell loads a small list of assemblies, this command will get the list of loaded assemblies:

[System.AppDomain]::CurrentDomain.GetAssemblies()

The syntax for calling a .NET class's static method is for the brackets go around the Class name and the double colon between the class and the static method

The output from that is a little hard to read lets just list the files involved

[AppDomain]::CurrentDomain.GetAssemblies() | foreach-object { split-path $_.Location -leaf } | sort

Here's the list of files the above command gives on my machine

Microsoft.PowerShell.Commands.Management.dll
Microsoft.PowerShell.Commands.Utility.dll
Microsoft.PowerShell.Commands.Utility.resources.dll
Microsoft.PowerShell.ConsoleHost.dll
Microsoft.PowerShell.ConsoleHost.resources.dll
Microsoft.PowerShell.Security.dll
Microsoft.PowerShell.Security.resources.dll
mscorlib.dll
System.Configuration.Install.dll
System.Data.dll
System.DirectoryServices.dll
System.dll
System.Management.Automation.dll
System.Management.Automation.resources.dll
System.Management.dll
System.ServiceProcess.dll
System.Xml.dll

As you can see SharePoint is not one of those in the list, also missing that could be useful is the System.Web assembly

There are a few ways to load an assembly onto your AppDomain but the easiest is to call the static method LoadWithPartialName on the Assembly class.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

LoadWithPartialName is great because you dont need to specify the full name to the assembly which in SharePoint v3 would be

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

and for MOSS

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

Now a lot of people will cry LoadWithPartialName has been deprecated as MS have got concerned how it has to make to many guesses over locating the assembly.
Personally I want to see a method that will simply load the latest version of the assembly I've told it to load, in a controlled environment like a SharePoint server thats exactly the behaviour I want to see. <rant> MS for god's sake just document the method properly and let devs choose whether to use it or not. </rant>

Anyway if you want to use the Load method take a look at this blog entry: http://www.leeholmes.com/blog/HowDoIEasilyLoadAssembliesWhenLoadWithPartialNameHasBeenDeprecated.aspx

Now you don't want to type the load commands each time so put them in your Powershell Profile script.
This changes location a lot depending on which version of Powershell your running so see this entry for the full details http://www.leeholmes.com/blog/TheStoryBehindTheNamingAndLocationOfPowerShellProfiles.aspx

So now what can we do with this?

Well first lets get a site collection we can play with

First create an SPSite object and pass it a valid site url


      $spsite= Microsoft.SharePoint.SPSite("http://portal.contoso.com")

to see the contents of the object just type the object on the command line

$spsite

To just get a list of the methods on the SPSite object

   $spsite | get-member -membertype method

To just get a list of the properties on the SPSite object

   $spsite | get-member -membertype property

To get a list of all sub sites

   $spsite.AllWebs

Get a list of webs ordered by the last time that the contents have been changed

   $spsite.allwebs | select LastItemModifiedDate, URL , Created | sort LastItemModifiedDate

There's lots more that you can do and I'll post them as I go.

 

Wednesday, October 25, 2006 10:01:53 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Related posts:
List Windows Groups in a SharePoint site and convert to DirectoryEntry
Update User Emails in SharePoint from Active Directory
PowerShell quickie: Extract the feature IDs used in large SharePoint projects
UK Community Day at Microsoft Reading
PowerShell: Generating a proxy for all the SharePoint WebServices
Silverlight 2: Unable to call SharePoint's Web Services
Tracked by:
"SharePoint and PowerShell " (Flexnet Consultants) [Trackback]
"Load a Custom DLL from PowerShell" (Precision Computing) [Trackback]
http://www.leeholmes.com/blog/LoadACustomDLLFromPowerShell.aspx [Pingback]
"SharePoint and PowerShell" (Mart Muller's Sharepoint Weblog) [Trackback]
http://blogs.tamtam.nl/mart/SharePointAndPowerShell.aspx [Pingback]
http://aggreg8.net/blogs/windows_powershell/archive/2006/10/27/load-a-custom-dll... [Pingback]
"Build a SharePoint Development Machine" (Eli Robillard's World of Blog.) [Trackback]
http://weblogs.asp.net/erobillard/archive/2007/02/23/build-a-sharepoint-developm... [Pingback]
"Buy cialis online generic cialis cheap cialis." (Cheap cialis online order cial... [Trackback]
"Buy cialis online pharmacy buy cialis." (Buy generic cialis.) [Trackback]
"Order cialis." (Order cialis online.) [Trackback]
"Cheapest generic cialis." (Buy generic cialis.) [Trackback]
"Brand ultram order ultram tramadol pain medicine." (Order tramadol.) [Trackback]
"Cheap cialis online pharmacy." (Buy cialis generic cheap cialis buy cialis.) [Trackback]
"And some else, come here..." (FlasheR!) [Trackback]
"SPI + PowerShell" (tom.shirley's blog) [Trackback]
"SPI + PowerShell" (SHAREPOINTBlogs.com Mirror) [Trackback]