Using enums in Powershell
Jason
Enum
 Enums are a very useful way to encode "options" in .NET programming.
 They offer a mechanism to represent a fixed ...
Using enums in Powershell
 Let's start examining this support by looking at the DayOfWeek enum.
PS> $now = Get-Date
PS> $...
Possible enum values
PS> [Enum]::GetNames( [System.DayOfWeek] )
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Creating enum instances
 The most direct way is to use the same "::" syntax used when accessing
static .NET
 Another opt...
Defining new enum types
 Let's look at how to define a brand new enum type within your Powershell
script
PS> $enumVal = [...
Add-Type
 The Add-Type cmdlet lets you define a .NET Framework class in your
Windows PowerShell session
Add-Type -TypeDefinition
PS>$source = @"
public class BasicTest
{
public static int Add(int a, int b)
{
return (a + b);
}
...
Add-Type -path
Add-Type -Path C:TempBasicTest.dll
Add-Type -Path C:TempBasicTest.cs
Defining new enum types
 If you don't give enum names explicit values, they will be automatically
numbered starting from ...
Defining new enum types
 You can also define specific integer values for each enum value
Add-Type -TypeDefinition @"
publ...
Defining new enum types
 If you want to provide explicit support for bitwise combinations of values
Add-Type -TypeDefinit...
Reference
 Using enums in Powershell
 http://latkin.org/blog/2012/07/08/using-enums-in-powershell/
 MSND Add-Type
 htt...
Upcoming SlideShare
Loading in...5
×

Powershell enum

3,469

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,469
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Powershell enum

  1. 1. Using enums in Powershell Jason
  2. 2. Enum  Enums are a very useful way to encode "options" in .NET programming.  They offer a mechanism to represent a fixed set of known values, named in a developer-friendly way.
  3. 3. Using enums in Powershell  Let's start examining this support by looking at the DayOfWeek enum. PS> $now = Get-Date PS> $now.DayOfWeek Thursday PS> $now | Get-Member DayOfWeek TypeName: System.DateTime Name MemberType Definition ---- ---------- ---------- DayOfWeek Property System.DayOfWeek DayOfWeek {get;}
  4. 4. Possible enum values PS> [Enum]::GetNames( [System.DayOfWeek] ) Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  5. 5. Creating enum instances  The most direct way is to use the same "::" syntax used when accessing static .NET  Another option is to cast a string containing a valid enum name into the enum type: PS> $enumVal = [System.DayOfWeek]::Monday PS> $enumVal = [System.DayOfWeek]::Sunday PS> $enumVal = [System.DayOfWeek] 'Sunday'
  6. 6. Defining new enum types  Let's look at how to define a brand new enum type within your Powershell script PS> $enumVal = [System.DayOfWeek] 'Sunday' Add-Type -TypeDefinition @" public enum SimpleEnumType { Value1, Value2, Value3 } "@ PS> [SimpleEnumType]::Value1 Value1
  7. 7. Add-Type  The Add-Type cmdlet lets you define a .NET Framework class in your Windows PowerShell session
  8. 8. Add-Type -TypeDefinition PS>$source = @" public class BasicTest { public static int Add(int a, int b) { return (a + b); } public int Multiply(int a, int b) { return (a * b); } } "@ C:PS> Add-Type -TypeDefinition $source
  9. 9. Add-Type -path Add-Type -Path C:TempBasicTest.dll Add-Type -Path C:TempBasicTest.cs
  10. 10. Defining new enum types  If you don't give enum names explicit values, they will be automatically numbered starting from 0. PS> [SimpleEnumType]::Value1 -as [int] 0 PS> [SimpleEnumType]::Value2 -as [int] 1 PS> [SimpleEnumType]::Value3 -as [int] 2
  11. 11. Defining new enum types  You can also define specific integer values for each enum value Add-Type -TypeDefinition @" public enum ExplicitEnumType { None = 0, Value1 = 1, Value2 = 10, Value3 = 100 } "@ PS> [ExplicitEnumType]::Value2 -as [int] 10
  12. 12. Defining new enum types  If you want to provide explicit support for bitwise combinations of values Add-Type -TypeDefinition @“ [System.Flags] public enum FlagsEnumType { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 } "@ PS> [FlagsEnumType] "Value1, Value3" Value1, Value3
  13. 13. Reference  Using enums in Powershell  http://latkin.org/blog/2012/07/08/using-enums-in-powershell/  MSND Add-Type  http://technet.microsoft.com/en-us/library/hh849914.aspx
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×