PowerShell and using .Net enum types

[NOTE: Because this page is the first hit in Google when you search on Powershell + enum, and I landed on this page too often myself, I decided to expand the page with some additional information] 

Scripting is heaven when you can utilize the complete .Net framework. One thing that was not directly clear for me was how to use enum values when calling .Net functions. It happened to be really easy, just cast the string representative of the enum value.

$myString = "/A/B/C//D/E//F/G"

$myParts = $myString.Split("/", [System.StringSplitOptions]"RemoveEmptyEntries")

results in an array of A,B,C,D,E,F,G

UPDATE: it happens to be even easier, you can say:

[System.Text.RegularExpressions.RegexOptions]::Singleline

And you can even binary-or them together:

[System.Text.RegularExpressions.RegexOptions]::Singleline -bor [System.Text.RegularExpressions.RegexOptions]::ExplicitCapture

It is also possible that an enum is defined within an enclosed type, in this case use [<namespace>.<enclosing type>+<nested type>]::EnumValue (thanks Alex)

For example:

[Microsoft.SharePoint.SPViewCollection+SPViewType]::Gantt

It is also possible to create a new real .net enum from PowerShell script. See http://blogs.msdn.com/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

And in PowerShell 2.0 you can do it even cleaner: http://thepowershellguy.com/blogs/posh/archive/2008/06/02/powershell-v2-ctp2-making-custom-enums-using-add-type.aspx

 

Published Monday, December 11, 2006 12:37 PM by svdoever
Filed under: ,

Comments

Monday, December 11, 2006 8:54 AM by CumpsD

# re: PowerShell and using .Net enum types

Have you tested it with bitwise added enums as well? :)

Like the regexoptions.

Enum1 | Enum2

My guess is it would work when passed as "Enum1 Enum2", but I'm not sure. Might be interesting to test since you're dealing with powershell and enums. (I haven't tried powershell yet)

Monday, June 16, 2008 4:16 PM by Alex

# re: PowerShell and using .Net enum types

Hi Serge,

thanks for this. I am trying to create a view on a SharePoint list using PowerShell. One of the parameters is the type of the view, and in C# it should be of type Microsoft.SharePoint.SPViewCollection.SPViewType. So that is an enum within a class.

This is what I've got:

PS> $asm = reflection.assembly]::loadwithpartialname("microsoft.sharepoint")

... stuff to initialise $splist and $spfields

PS> $splist.Views.Add("New view", $viewFields, '<OrderBy><FieldRef Name="ID" /></OrderBy>', 100, 1, 1, [Microsoft.SharePoint.SPViewCollection.SPViewType] "Html", 0)

The last line gives the error "Unable to find type [Microsoft.SharePoint.SPViewCollection.SPViewType]: make sure that the assembly containing this type is loaded."

If I try to fetch the type from the assembly like this:

PS > $asm.GetType("Microsoft.SharePoint.SPViewCollection.SPViewT

ype", 1, 1)

Exception [...] "Could not load type 'Microso

ft.SharePoint.SPViewCollection.SPViewType' from assembly 'Microsoft.SharePoint,

Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'."

In contrast the following works:

PS> $asm.GetType("Microsoft.SharePoint.SPViewCollection", 1, 1)

I can't get this to work, perhaps you have some tips?

Thanks.

Alex

Tuesday, June 17, 2008 3:43 AM by Alex

# re: PowerShell and using .Net enum types

Found it: nested types should be referenced using <namespace>.<enclosing type>+<nested type>

i.e. in my case

[Microsoft.SharePoint.SPViewCollection+SPViewType]::Gantt

Cheers,

Alex

Wednesday, September 24, 2008 2:00 PM by James Kendig

# re: PowerShell and using .Net enum types

Can you please post your final code for this piece including the value of your variable $viewfields.

I am trying to create a new Calendar View inside of a Calendar List and have not been able to successfully figure out the syntax.

Thanks.

James

Leave a Comment

(required) 
(required) 
(optional)
(required)