Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SharePoint and PowerShell

268 views
Skip to first unread message

Jerry Rasmussen

unread,
Oct 23, 2007, 2:05:27 PM10/23/07
to
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://tw-lt-9300-300”)
$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

Marco Shaw [MVP]

unread,
Oct 24, 2007, 10:27:59 AM10/24/07
to

I'll see if I can find a 2003 test server (either VM or VHD). Might
have to load that assembly into Reflector to do some spelunking...

Info on CLS:
http://msdn2.microsoft.com/en-us/library/bhc3fa7f(VS.71).aspx

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

Marco Shaw [MVP]

unread,
Oct 24, 2007, 10:32:51 AM10/24/07
to

> I'll see if I can find a 2003 test server (either VM or VHD). Might
> have to load that assembly into Reflector to do some spelunking...
>
> Info on CLS:
> http://msdn2.microsoft.com/en-us/library/bhc3fa7f(VS.71).aspx

OK:
http://blogs.devhorizon.com/blogs/reza_on_blogging/archive/2007/07/30/489.aspx

Looks like you're out of luck. You will not be able to get that $site
property.

Marco

Oisin Grehan

unread,
Oct 24, 2007, 12:40:11 PM10/24/07
to

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.

Jerry Rasmussen

unread,
Oct 24, 2007, 3:13:24 PM10/24/07
to
Wow that's quite a lot to take in. Thanks for your help and the in
site. I am fairly new to PowerShell but so far it rocks.

Also thanks to Marco for his response.

Marco Shaw [MVP]

unread,
Oct 24, 2007, 3:30:52 PM10/24/07
to
> 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.

Thanks for the details!

Marco

Jerry Rasmussen

unread,
Oct 26, 2007, 3:39:25 PM10/26/07
to
Just wanted to give a quick follow up. When doing a
getmethod("get_xyz") The xyz is case sensitive So I was trying to get
the server name get_server does not work but get_Server does. Just an
FYI in case anyone comes across this post.
0 new messages