SlideShare uma empresa Scribd logo
1 de 13
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 set of known values, named in
a developer-friendly way.
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;}
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 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'
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
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);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}
"@
C:PS> Add-Type -TypeDefinition $source
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 0.
PS> [SimpleEnumType]::Value1 -as [int]
0
PS> [SimpleEnumType]::Value2 -as [int]
1
PS> [SimpleEnumType]::Value3 -as [int]
2
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
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
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

Mais conteúdo relacionado

Mais procurados (20)

Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Sequence Types in Python Programming
Sequence Types in Python ProgrammingSequence Types in Python Programming
Sequence Types in Python Programming
 
Open course(programming languages) 20150225
Open course(programming languages) 20150225Open course(programming languages) 20150225
Open course(programming languages) 20150225
 
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
codin9cafe[2015.02.25]Open course(programming languages) - 장철호(Ch Jang)
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Pipes and filters
Pipes and filtersPipes and filters
Pipes and filters
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho dictionaries
 
1. python
1. python1. python
1. python
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Python overview
Python   overviewPython   overview
Python overview
 
Teeing Up Python - Code Golf
Teeing Up Python - Code GolfTeeing Up Python - Code Golf
Teeing Up Python - Code Golf
 
Functions
FunctionsFunctions
Functions
 
lab4_php
lab4_phplab4_php
lab4_php
 
Java 1-contd
Java 1-contdJava 1-contd
Java 1-contd
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 

Destaque

Tipo music-ui-規劃原則
Tipo music-ui-規劃原則Tipo music-ui-規劃原則
Tipo music-ui-規劃原則LearningTech
 
2013 jsdc webworker
2013 jsdc webworker2013 jsdc webworker
2013 jsdc webworkerBingo Yang
 
Ken 20150306 心得分享
Ken 20150306 心得分享Ken 20150306 心得分享
Ken 20150306 心得分享LearningTech
 
Learning tech week_1_james
Learning tech  week_1_jamesLearning tech  week_1_james
Learning tech week_1_jamesLearningTech
 
Introduction of lambda expression and predicate builder
Introduction of lambda expression and predicate builderIntroduction of lambda expression and predicate builder
Introduction of lambda expression and predicate builderLearningTech
 
Raphael JavaScript Library
Raphael JavaScript LibraryRaphael JavaScript Library
Raphael JavaScript LibraryLearningTech
 

Destaque (7)

Tipo music-ui-規劃原則
Tipo music-ui-規劃原則Tipo music-ui-規劃原則
Tipo music-ui-規劃原則
 
2013 jsdc webworker
2013 jsdc webworker2013 jsdc webworker
2013 jsdc webworker
 
Ken 20150306 心得分享
Ken 20150306 心得分享Ken 20150306 心得分享
Ken 20150306 心得分享
 
Learning tech week_1_james
Learning tech  week_1_jamesLearning tech  week_1_james
Learning tech week_1_james
 
mongoose
mongoosemongoose
mongoose
 
Introduction of lambda expression and predicate builder
Introduction of lambda expression and predicate builderIntroduction of lambda expression and predicate builder
Introduction of lambda expression and predicate builder
 
Raphael JavaScript Library
Raphael JavaScript LibraryRaphael JavaScript Library
Raphael JavaScript Library
 

Semelhante a Powershell enum

Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Power shell examples_v4
Power shell examples_v4Power shell examples_v4
Power shell examples_v4JoeDinaso
 
Lecture 6 Enumeration in java ADVANCE.pptx
Lecture 6 Enumeration in java ADVANCE.pptxLecture 6 Enumeration in java ADVANCE.pptx
Lecture 6 Enumeration in java ADVANCE.pptxAbdulHaseeb956404
 
define a class name Employee whose objects are records for employee..pdf
define a class name Employee whose objects are records for employee..pdfdefine a class name Employee whose objects are records for employee..pdf
define a class name Employee whose objects are records for employee..pdffashioncollection2
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdfICADCMLTPC
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docxajoy21
 

Semelhante a Powershell enum (20)

Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
The Power of PowerShell: Advanced
The Power of PowerShell: Advanced The Power of PowerShell: Advanced
The Power of PowerShell: Advanced
 
Power shell examples_v4
Power shell examples_v4Power shell examples_v4
Power shell examples_v4
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
 
Lecture 6 Enumeration in java ADVANCE.pptx
Lecture 6 Enumeration in java ADVANCE.pptxLecture 6 Enumeration in java ADVANCE.pptx
Lecture 6 Enumeration in java ADVANCE.pptx
 
define a class name Employee whose objects are records for employee..pdf
define a class name Employee whose objects are records for employee..pdfdefine a class name Employee whose objects are records for employee..pdf
define a class name Employee whose objects are records for employee..pdf
 
C# p8
C# p8C# p8
C# p8
 
C# p9
C# p9C# p9
C# p9
 
Developer Guide
Developer GuideDeveloper Guide
Developer Guide
 
Methods.ppt
Methods.pptMethods.ppt
Methods.ppt
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
Class 10
Class 10Class 10
Class 10
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
java script
java scriptjava script
java script
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
2. Create a Java class called EmployeeMain within the same project Pr.docx
 2. Create a Java class called EmployeeMain within the same project Pr.docx 2. Create a Java class called EmployeeMain within the same project Pr.docx
2. Create a Java class called EmployeeMain within the same project Pr.docx
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 

Mais de LearningTech

Mais de LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Último

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Powershell enum

  • 1. Using enums in Powershell Jason
  • 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. 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. Possible enum values PS> [Enum]::GetNames( [System.DayOfWeek] ) Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  • 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. 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. Add-Type  The Add-Type cmdlet lets you define a .NET Framework class in your Windows PowerShell session
  • 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. Add-Type -path Add-Type -Path C:TempBasicTest.dll Add-Type -Path C:TempBasicTest.cs
  • 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. 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. 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. 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