Travis Lingenfelder

Travis is a senior consultant for Catapult Systems and blogs about Microsoft technologies including SharePoint, but is passionate about all things relating to technology. Travis is a MCTS for SharePoint Server 2007, application development and configuration.
RSS Feed

Using non-CLS compliant types in PowerShell

Not all classes in the SharePoint API are CLS-compliant types.  While using a language like C#.NET it does not matter since the .NET framework knows how to work with these classes.  Working in PowerShell is a completely different story.  PowerShell does not differentiate between the various cases of names of objects, properties, or methods.  This can become apparent rather quickly when, for example, you want to change the maximum site collections allowed in a content database by using a PowerShell script.  This may seem like a relatively simple task since the SPContentDatabase type has a property called MaximumSiteCount of type int32, right?

The problem is that the SPContentDatabase has two methods for the id property (ID and Id) making it non-CLS compliant – PowerShell can’t differentiate between ID and Id.  As soon as an instance of an SPContentDatabase is referenced, you will get an ugly message like:

$siteCol.ContentDatabase.MaximumSiteCount = 1
The field/property: "Id" for type: "Microsoft.SharePoint.Administration.SPContentDatabase" differs only in case from the field/property: "ID". Failed to use non CLS compliant type.

The way that you have to work around this issue is to get a handle to the type’s property using reflection and then invoke the SetValue() method.

$maxProp = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("MaximumSiteCount")
$maxProp.SetValue($siteCol.ContentDatabase, 1, $null)

If you want to get the property value:

$returnVal = $maxProp.GetValue($siteCol.ContentDatabase, $null)

To invoke a method of the non-CLS compliant type you must get a handle to the method instance and use Invoke.  The following example gets a reference to a method that has multiple overrides and gets the instance of the method that takes no parameters.

$update = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("Update", @())
$update.Invoke($newSite.ContentDatabase, "instance,public", $null, $null, $null)

If you want to get the instance of a method that uses parameters, you must get the handle to the method using an array of types that match the signature of the overridden method.  The following example gets the instance of a method that takes a Boolean parameter.

$boolType = [System.Boolean]
$typeArray = , $boolType
$trueArray = , $true
$update = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("Update”, $typeArray)
$update.Invoke($newSite.ContentDatabase, "instance,public", $null, $trueArray, $null)

To complete this example, below is the snippet of PowerShell script that will set parameters on an SPContentDatabase and update it.

$warnProp = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("WarningSiteCount")
$warnProp.SetValue($newSite.ContentDatabase, 0, $null)

$maxProp = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("MaximumSiteCount")
$maxProp.SetValue($newSite.ContentDatabase, 1, $null)

$update = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("Update", @())
$update.Invoke($newSite.ContentDatabase, "instance,public", $null, $null, $null)

Posted by Travis Lingenfelder on Friday, 26 Jun 2009 02:21
3 Comments | Filed under: PowerShell, SharePoint
Bookmark this post with:        

Links to this post

A Guide to SharePoint Performance Testing (Part I) – What is “Acceptable” Anyway?

Trackback from  David Broussard  on  M/d/yyyy



Comments

On 01 Mar 2010 09:07, Marco said:

Thanx, just what i needed!

On 13 Mar 2010 06:20, George Bonney said:

Many thanks - this solved my problem very quickly.

On 27 Apr 2010 06:32, Florian said:

Man, you helped me out. Very usefull posting !!!

Leave a comment

Name (required)

Url

Email

Comments

Complete this section to post your comment