Overview
There’s a PSBlogWeek going on, with some great posts and fun topics to get your thoughts churning! Adam Bertram wrote a nice article on dynamic parameters - these are certainly fun, but I’ve run into issues with dynamic parameters in the past, and generally try to avoid them unless absolutely necessary.
This morning, PowerShell.com posted a great tip: Clever Parameter Validation. Long story short, you can use ValidateSet or Enums to improve user experience. But how do you find enums? And how do you know what they enumerate?
Finding Enums
The first thing we need to do is find those Enums! Check out Get-Type on the Technet Gallery for a simple function to help find these.
Behind the scenes, we use .NET reflection. We call [AppDomain]::CurrentDomain.GetAssemblies()
, run GetExportedTypes()
on the resulting objects, and filter the results based on parameters the user provides. These words and the syntax we use confuse me, so I use Get-Type.
Download the ps1, load it up, and we can start to discover all the available types in your session:
#Add Get-Type to your session:
. "\\Path\To\Get-Type.ps1"
#Get help for Get-Type
Get-Help Get-Type -Full
#List all enums in your session
Get-Type -IsEnum
Nice! Maybe I’m working with EPPlus and Excel through PowerShell and want to see what enums the EPPlus library offers:
Get-Type -IsEnum -Module EPPlus.dll
Very cool! I get a list of all enums in the EPPlus library. Maybe I want to offer a parameter that lists chart types, without manually speciying a ValidateSet that may need changing down the line:
Get-Type -IsEnum -Module EPPlus.dll -FullName *chart*
eChartType sounds interesting, but how do I know if this is what I want? What options will it give me?
Exploring Enums
Looking at the values behind an enum is fairly straightforward, we can use the GetValues method of System.Enum. Let’s look at the enum values for DayOfWeek:
[enum]::GetValues( 'System.DayOfWeek' )
.NET is quite powerful and a fantastic tool to have in your toolbelt. Unfortunately, I’m forgetful at times, and prefer the friendly verb-noun names of PowerShell functions, along with the abstraction they give us. Here’s a quick function to pull out those enum values, without all the .NET syntax, and which can take pipeline input:
Dot source or paste this function into your session, and let’s see if we can pull out some enum values for chart types:
Get-Type -IsEnum -Module EPPlus.dll -FullName *eChartType | Get-EnumValues
Awesome! I can use this as a parameter in PSExcel’s Add-PivotTable and Export-XLSX, thanks to the idea and code from Doug Finke.
This was just one example, there are plenty of enums out there that you can take advantage of:
Explore!
That’s about it, this should give you enough tools to go out and explore the types and enums you have available to you! You might even consider creating your own - this will get easier in PowerShell 5, but I prefer down-level compatibility.
I walked through a few helpful toys. If you want to get into some power tools, and .NET doesn’t scare you, check out tools like ILSpy and Oisin Grehan’s Poke. If you aren’t working with .NET through PowerShell yet, don’t let these scare you off; most of us (inculding myself) don’t need to dive into the weeds as deeply as these allow.
As scary as .NET is, at some point you will likely run into something you simply can’t do without it. Thankfully, the Internet is riddled with examples and helpful tidbits on how to do this - good luck!