# Wednesday, October 24, 2007

Someone on the newsgroup ran into an issue whereby a instance of a non CLS Compliant type was fed to PowerShell's default formatter. The type in question had two properties with the same name, but in different casing. I'm pretty much just going to carbon-copy the qestion and answer here as I feel this is worth preserving in the annals of blogdom.

> I am in the process of writing a script to document SharePoint
> installations.  When I try to get information about the content database
> I get this error.

> "out-lineoutput : 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."

> Here is some of the PowerShell Script that I am using

> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
> $site = New-Object Microsoft.SharePoint.SPSite("http://localhost")
> $site.ContentDatabase

> It is the final line that is giving me the error.  I can get other
> properties without error.  Can someone enlighten me on this error
> message and let me know if there is a way to work around it.

> Thanks
> Jerry

Hi Jerry,

This is a tricky one for people very new to powershell without any real experience of the .NET framework. One of the rules of a so-called "CLS compliant type" (cls = common lanaguage specification) is that you should not expose any public members that only differ in casing. The SPContentDatabase type has two properties, "Id" and "ID." This is perfectly legal in C# since case in important in that language and the compiler sees them as different as chalk and cheese. However, in VB.NET, case is not important and as such it [vb] has no native mechanism to differentiate between the two. The same goes for PowerShell's grammar. So, how do we get around this?

First, you grab a handle to the method directly by asking for it from the Type itself:

PS> $idMethod1 = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_ID")

and for the second version,

PS> $idMethod2 = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_Id")

Note the differing case for get_Id and get_ID. Btw, Properties like ID in .NET are actually served by special accessor methods prefixed with get_ and set_, hence the prefixes. Next, you need a way to invoke those methods on the instance itself: this is done by using the Invoke method on the MethodInfo object representing the method itself. You pass the instance to the Invoke method telling it that the method is "instance" (e.g. not static/shared) and "public" and it will call that method for you on the instance, returning the value, if any.

PS> $result = $idMethod1.Invoke($site.ContentDatabase, "instance,public", $null, $null, $null)

I know from the SharePoint documenation, that this property returns a guid. Also, due to another oversight, Posh does not know how to format Guids, so you must call ToString() on the result to get the value back or you'll be greeted by a blank line from powershell's formatter.

PS> $result.ToString()
1ade149d-2049-4668-b555-4b7be9374c65

Bizarrely, these two properties return the same guid, but hey, I'll let someone from the sharepoint team answer that one. Anyway, you should have enough information here to extract the properties that you need.

So, that's the interesting portion out of the way. Another way to do this might be to create a custom view for the SPContentDatabase type omitting the duplicated property and use the Update-FormatData cmdlet to activate it. This may not work though, as the non-cls compliancy issue may rear it's ugly head before posh's formatter gets this far.

Hope this helps,

- Oisin / x0n.

posted on Wednesday, October 24, 2007 4:18:50 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
Related posts:
PowerShell v2.0 – Differences Between CTP3/Win7Beta and Win7RC
Pscx 1.2 Beta Released
PowerShell 2.0 CTP3: Modules, in Practice - Closures
PowerShell 2.0 CTP3: Modules, in Practice – .NET Interfaces
PowerShell 2.0 CTP3: Modules, in Practice.
Visual Studio 2008 Extensions for WSS 3.0 v1.3 March CTP

Referred by:
powershell (search.live.com) [Referral]
powershell "failed to use non CLS compliant type" (www.google.com) [Referral]
powershell failed to use non cls compliant type (www.google.com) [Referral]
Microsoft.SharePoint.Administration.SPContentDatabase powershell (www.google.com) [Referral]
compliant type (tw.search.yahoo.com) [Referral]
http://tw.babelfish.yahoo.com/translate_url?doit=done&tt=url... [Referral]
cls-compliant guid (www.google.com) [Referral]
differs only in case from the field/property (search.live.com) [Referral]
powershell cls (www.google.com) [Referral]
cls compliant powershell (search.live.com) [Referral]
CLSCompliant Methods (www.google.com) [Referral]
sharepoint cls-compliant (www.google.fr) [Referral]
sharepoint cls-compliant (www.google.ch) [Referral]
.NET Framework types are CLS-compliant (www.google.com) [Referral]
CLS-compliant moss (www.google.se) [Referral]
powershell spcontentdatabase (www.google.com) [Referral]
cls compliant types (www.google.com) [Referral]
powershell sharepoint contentdatabase (www.google.fr) [Referral]
powershell clscompliant (www.google.co.in) [Referral]
non cls compliant types (search.live.com) [Referral]
moss GetMethod MethodInfo invokemethod (www.google.fi) [Referral]
"only in case from the field/property" (www.google.se) [Referral]
sharepoint features powershell activate (www.google.com) [Referral]
.net type cls (www.google.pl) [Referral]
Vb.net non CLS compliant (www.google.com) [Referral]
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 (search.live.com) [Referral]
powershell invokemethod (www.google.com) [Referral]
powershell cls compliant (www.google.com) [Referral]
Example of Non-CLS types (www.google.com) [Referral]
cls compliant types list (www.google.com) [Referral]
powershell Failed to use non CLS compliant type (search.live.com) [Referral]
The field/property: "Id" for type: "Microsoft.SharePoint.Administration.SPContentDatabase" (www.google.com) [Referral]
spcontentdatabase powershell (search.live.com) [Referral]
"The field/property: "Id" for type:" (search.live.com) [Referral]
out-lineoutput : 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. (www.google.ca) [Referral]
http://209.85.201.101/translate_c?hl=en&sl=it&u=http://blogs... [Referral]
spcontentdatabase (search.live.com) [Referral]
PowerShell SPContentDatabase Id ID (www.google.co.jp) [Referral]
what is CLS-compliant +sharepoint (www.google.se) [Referral]
powershell activate sharepoint feature (www.google.co.uk) [Referral]
powershell spcontentdatabase (www.google.be) [Referral]
contentdatabases cls compliant (www.google.be) [Referral]
powershell "Failed to use non CLS compliant type." (www.google.com) [Referral]
"cls compliant types" (www.google.com) [Referral]
powershell failed to use a non cls compliant (www.google.de) [Referral]
cache:GFr1-IYgGZcJ:www.nivot.org/2007/10/24/NonCLSCompliantTypesInPowerShell.aspx "IPHeaderLength". Failed to use non CLS compliant type. (209.85.229.132) [Referral]
"IPHeaderLength". Failed to use non CLS compliant type. (www.google.com) [Referral]
powershell "Failed to use non CLS compliant type" (www.google.fi) [Referral]
powershell cls compliant (www.google.de) [Referral]
format-default : the field/property: "Id" for type: "microsoft (www.google.com) [Referral]
format-default the field/property: "id" failed to use non cls compliant type (www.google.com) [Referral]
"GetMethod"+"not static"+"vb"+"invoke" (www.google.com.br) [Referral]
powershell cls (www.google.nl) [Referral]
powershell out-lineoutput CLS (www.google.co.jp) [Referral]
PowerShell SPContentDatabase (www.google.co.jp) [Referral]
209.85.201.101 (www.google.com) [Referral]
powershell Failed to use non CLS compliant type (search.live.com) [Referral]
http://tw.babelfish.yahoo.com/translate_url?doit=done&tt=url&trurl=http%3A%2F%2Fwww.zuowen5.com%2Fhu (www.ask.com) [Referral]
NON CLS-Compliant method (www.google.com) [Referral]
moss content database powershell Failed to use non CLS compliant type (search.live.com) [Referral]
compliant type (www.google.com) [Referral]
The field/property differs only in case from the field/property Failed to use non CLS compliant type. (www.google.sk) [Referral]
CLS Compliant properties (www.google.se) [Referral]
Failed to use non cls compliant type in Powershell (www.google.com) [Referral]
Powershell CLS compliant (www.google.com) [Referral]
Microsoft.SharePoint.Administration is not CLS compliant (www.google.co.in) [Referral]
Failed to use non CLS compliant type + powershell (www.google.co.uk) [Referral]
powershell cls compliance (www.google.co.uk) [Referral]
Get a List of Content Databases with PowerShell (www.google.com) [Referral]
powershell ContentDatabase CLS (www.google.com) [Referral]
CLS-Compliant types (www.google.com) [Referral]
powershell clscompliant (www.google.com) [Referral]
visual basic let get cls invoke (www.google.es) [Referral]
property type CLS-compliant (www.google.it) [Referral]
cls .net not complient types (www.google.com) [Referral]
SPContentDatabase powershell (www.google.com) [Referral]
new spcontentdatabase (www.google.co.in) [Referral]
sharepoint what is cls compliance (www.google.com.au) [Referral]
powershell SPContentDatabase (www.google.com) [Referral]
CLS-compliant (www.google.com) [Referral]
powershell activate feature (www.google.com) [Referral]
cls-compliant type (www.google.cn) [Referral]
powershell cls (www.google.de) [Referral]
powershell cls-compliant (www.google.ru) [Referral]
PowerShell SPContentDatabase (www.google.com) [Referral]
Cmdlet is not CLS-compliant (www.google.ca) [Referral]
"activate sharepoint feature" (www.google.com.au) [Referral]
SharePoint non CLS compliant objects list (www.google.co.in) [Referral]
sharepoint powershell ID non cls compliant (search.live.com) [Referral]
SPContentDatabase PowerShell (search.live.com) [Referral]
SPContentDatabase PowerShell (www.google.com) [Referral]
"sharepoint designer" cls compliant (www.google.com) [Referral]
get contentdatabase in moss in powershell (www.google.com) [Referral]
sharepoint "is not CLS-compliant" (www.google.ru) [Referral]
the field/property Id for type differs only in case from the field/property (www.google.com) [Referral]
SPSite not CLS compliant (www.google.es) [Referral]
create content database and powershell (www.google.co.uk) [Referral]
"The field/property: "Id" for type: "Microsoft.SharePoint.Administration.SPContentDatabase" differs only in case from the field/property: "ID"." (www.google.fi) [Referral]
out-lineoutput : The field/property: "Id" for type: "Microsoft.SharePoint.Administration.SPContentDatabase" differs onl y in case from the field/property: "ID". Failed to use non CLS compliant type. (www.google.com) [Referral]
Microsoft.SharePoint.Administration.SPContentDatabase returns no value in vb.net application (www.google.com) [Referral]
getmethod sharepoint powershell (www.google.fi) [Referral]
powershell The field/property differs only in case from the field/property (www.google.si) [Referral]
cls compliant guid (www.google.com.au) [Referral]
http://www.google.com/reader/view/?tab=my [Referral]
powershell cls (www.google.com.mx) [Referral]
Microsoft.SharePoint.Administration.SPContentDatabase" differs only (www.google.com) [Referral]
http://64.233.189.132/translate_c?hl=en&sl=it&u=http://blogs... [Referral]
+powershell +"failed to use non cls compliant type" (www.google.com) [Referral]
wss 3.0 "not cls compliant" (www.google.com) [Referral]
powershell CLS compliant (www.google.co.uk) [Referral]
powershell ContentDatabases (www.google.co.uk) [Referral]
differs only in case from the field/property (www.google.co.jp) [Referral]
non clscompliant types (www.google.co.in) [Referral]
nivot ink content database (www.google.be) [Referral]
sharepoint: CLS Compliant? (www.google.com) [Referral]
powerhsell content database cls (www.google.be) [Referral]
(SPSite "not CLS-compliant" (www.google.com) [Referral]
list spcontentdatabase powershell (www.google.com) [Referral]
sharepoint not cls-compliant visual studio (www.google.com) [Referral]
CLSCompliant property (www.google.it) [Referral]
SPContentdatabase content databases powershell SharePoint (www.google.com) [Referral]
powershell get contentdatabase (www.google.com) [Referral]
cls-compliant types (www.google.co.in) [Referral]
SharePoint "Non-CLS" (www.google.com) [Referral]
out-lineoutput field/property for type differs only in case failed to use a non cls compliant type (www.google.com) [Referral]
cache:GFr1-IYgGZcJ:www.nivot.org/2007/10/24/NonCLSCompliantTypesInPowerShell.aspx failed to use non CLS compliant type (74.125.153.132) [Referral]
differs only in case from the field/property: "ID" (www.google.com) [Referral]
content database cls (www.google.be) [Referral]
powershell failed to use a non cls compiant type contentdatabase (www.google.com) [Referral]
powershell failed to use a non cls compliant type set property (www.google.com) [Referral]
powershell failed to use a non cls compiant type (www.google.com) [Referral]
Powershell "content database" (www.google.co.uk) [Referral]
visual studio 2008 workflow cls (www.google.ca) [Referral]
non CLS complient types (www.bing.com) [Referral]
Comments are closed.