Warren F bio photo

Warren F

Systems Engineer with a penchant for PowerShell, science, cooking, information security, family, cookies, and the Oxford comma.

Connect

@pscookiemonster LinkedIn Github Stackoverflow TechNet RSS Feed My old blog

My GitHub Repos

AppVReporting BuildHelpers Citrix.NetScaler Git-Presentation InfoBlox Invoke-Parallel PowerShell PSDepend PSDeploy PSDiskPart PSExcel PSHTMLTable PSRabbitMQ PSSlack PSSQLite PSStash RabbitMqTools SecretServer

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

IsEnum output

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

IsEnum module output

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*

IsEnum fullname output

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' )

Enum GetValues

.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

eChartType values

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.

Add-PivotTable -ChartType

This was just one example, there are plenty of enums out there that you can take advantage of:

Add-PivotTable -ChartType

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!