Wouldn’t it be nice if parameters would suggest valid arguments for the user? Sometimes they do. When you type below command and press a SPACE after -LogName, PowerShell ISE and Visual Studio Code pop up an IntelliSense menu with all log files you can dump:

 
PS> Get-EventLog -LogName 
 

If no automatic IntelliSense pops up (i.e. in the PowerShell console), you can press TAB for auto completion, or CTRL+SPACE to manually force the IntelliSense choices to appear.

You can do the same with your own PowerShell functions, and there are a number of ways to do this. Today let’s look at using enumeration types.

When you assign an enumeration type to your parameter, it automatically lists the available values. The code below uses the [System.ConsoleColor] type which lists all valid console colors:

function Set-ErrorColor 
{
    param(
        [Parameter(Mandatory)]
        [System.ConsoleColor]
        $Color
    )

    $Host.PrivateData.ErrorForegroundColor = [string]$Color
}

When you call Set-ErrorColor, PowerShell automatically suggests the valid console colors to you. When you pick one, the function assigns this color to the error foreground color. If you don’t like the harsh red error color, smooth it down by turning error messages to green:

 
PS> Set-ErrorColor -Color Green

PS> 1/0
Attempted to divide by zero.
At line:1 char:1
+ 1/0
+ ~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException  
 

Side note: it is up to you what you do with the selected type. Sometimes, it may be clever to convert it to a different type. In the example above, the chosen color is converted to string, for example. Why?

Because only the PowerShell console window supports ConsoleColor colors. The PowerShell ISE editor, for example, supports many more colors and uses a [System.Windows.Media.Color] type.

Since strings can be converted to both types, but ConsoleColor cannot directly convert to Windows.Media.Color, you can use the user input both in consoles and the PowerShell ISE by converting it to the more generic type string:

 
# string converts to ConsoleColor
PS> [ConsoleColor]'red'
Red

# string converts to System.Windows.Media.Color
PS> [System.Windows.Media.Color]'red'


ColorContext : 
A            : 255
R            : 255
G            : 0
B            : 0
ScA          : 1
ScR          : 1
ScG          : 0
ScB          : 0

# ConsoleColor DOES NOT convert to System.Windows.Media.Color
PS> [System.Windows.Media.Color][ConsoleColor]'red'
Cannot convert value "Red" to type "System.Windows.Media.Color". Error: "Invalid cast from 'System.ConsoleColor' to 
'System.Windows.Media.Color'."
At line:1 char:1
+ [System.Windows.Media.Color][ConsoleColor]'red'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible
 

You are a PowerShell Professional, passionate about improving your code and skills? You take security seriously and are always looking for the latest advice and guidance to make your code more secure and faster? You’d love to connect to the vibrant PowerShell community and get in touch with other PowerShell Professionals to share tricks and experience? Then PowerShell Conference EU 2020 might be just the right place for you: https://psconf.eu (June 2-5, 2020 in Hanover, Germany).

It’s a unique mixture of classic conference with three parallel tracks filled with fast-paced PowerShell presentations, and advanced learning class with live discussions, Q&A and plenty of networking.

Secure your seat while they last: https://psconf.eu/register.html. The speakers and agenda is available here: https://psconf.eu/schedule.

Twitter This Tip! ReTweet this Tip!

Anonymous