SharePoint Tidbit – Getting the error “Failed to use non CLS compliant type”


Hello All,

My customer is currently running MOSS 2007 and as we prepare for there migration, we have created several PowerShell scripts to collect data.

One of the script get’s the content database object and when I tried to get the property name of that database I got this error

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.
At line:9 char:14
+     $DBName = <<<<  $SiteContentDB.GetProperty(“Name”)
+ CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : NotACLSComplaintProperty

The reason for this error is that PowerShell does not know how to handle the fact that the SPContentDatabase class has two properties with the same name but different case….’ID’.  So it just errors out and gracefully continues on but doesn’t give you the name of the database.

Luckly this is very easy to resolve, the script originally looked something like this

[void][System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$WebApplicationUrl = “http://moss2007”
$WebApplication = New-Object Microsoft.SharePoint.SPSite($WebApplicationUrl)
$Sites = $WebApplication.WebApplication.Sites
ForEach($Site in $Sites)
{
$SiteContentDB = $Site.ContentDatabase
$DBName = $SiteContentDB.Name
$SiteUrl = $Site.Url
Write-host “$SiteContentDB,$SiteUrl”
Write-Host “DB Name is $DBName”
}
And this is what your script should look like
[void][System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$WebApplicationUrl = “http://moss2007”
$WebApplication = New-Object Microsoft.SharePoint.SPSite($WebApplicationUrl)
$Property_Database_Name = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty(“Name”)
$Sites = $WebApplication.WebApplication.Sites
ForEach($Site in $Sites)
{
$SiteContentDB = $Site.ContentDatabase
$DBName = $Property_Database_Name.GetValue($Site.ContentDatabase,$null)
$SiteUrl = $Site.Url
Write-host “$SiteContentDB,$SiteUrl”
Write-Host “DB Name is $DBName”
}
What we have done is added the line to grab the Property, then changed the line to grab the value of that property.
Pax
Comments (0)

Skip to main content