PowerShell – Enum – System. Color

Background

This last week I have been working hard on a PowerShell Script.

The area of programming is one that I have very little experience in.

And, for that reason, I really could not afford to be sloppy and so followed sample codes religiously.

Some of the Sample Codes rely on enum classes and I found that dependability useful.

Enum

What are enums?

Enums are a set of named values in a set.

Offer me Some Samples

Colors

  1. Humans :- White, Black.
  2. Traffic Light :- Green, Yellow, and Red

Direction

  1. North, South, East, and West

Day of the Week

  1. SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY

Benefits

  1. Type Safety
  2. Consolidate a set of constants in one place
    • Distinctiveness

Code

Sample


Set-StrictMode -Version 1.0


Function listSystemConsoleColor()
{

    [string] $log=""
    [string] $formatLog=""

    #Define String Array
    [string[]] $collectionColor=$null;

    [string] $colorCurrent="";

    [enum]   $enumColorCurrent = $null;

    [int]    $enumColorCurrentAsInt=0;

    $formatLog="`t{0} - Numeric formatted {1:d} // Casted {2}";

    $log = "`r`n"
    Write-Host $log -ForegroundColor White; 

    $log = "System.ConsoleColor";
    Write-Host $log -ForegroundColor White; 

    # Get System.ConsoleColor Names
    $collectionColor = [System.Enum]::GetNames( `
                          'System.ConsoleColor' `
                        );

    # Iterate List
    $collectionColor | ForEach-Object{

        $colorCurrent = $_

        $enumColorCurrent = [Enum]::Parse( `
                                  [ConsoleColor] `
                                , $colorCurrent `
                            );

        $enumColorCurrentAsInt = [int] $enumColorCurrent;

        $log = $formatLog -f `
                      $colorCurrent `
                    , $enumColorCurrent `
                    , $enumColorCurrentAsInt;

        Write-Host $log -ForegroundColor $enumColorCurrent; 

    }
}

[string] $log=""
[string] $formatLog="";
#[enum] [System.ConsoleColor]$colorWhite=$null;
[enum]   $colorWhite=$null;
[string] $strColorRed = "";
[enum]   $enumColorRed = $null;

$formatLog="State {0} => {1} - Numeric formatted {2:d} // Casted {3}`r`n"

Write-Host

#hardcode white
$colorWhite = [ConsoleColor]::White;

$log = $formatLog -f `
                  "Initial" `
                , $colorWhite `
                , $colorWhite `
                , [int] $colorWhite;

Write-Host $log -ForegroundColor $colorWhite

$strColorRed = "Red";

$enumColorRed = [Enum]::Parse( `
                                [ConsoleColor] `
                              , $strColorRed `
                             );

$log = $formatLog -f "Variable" `
                        , $strColorRed `
                        , $enumColorRed `
                        , [int] $enumColorRed;

Write-Host $log -ForegroundColor $enumColorRed

$null = listSystemConsoleColor

Output

Output – Text


>powershell ./enum.color.ps1

State Initial => White - Numeric formatted 15 // Casted 15

State Variable => Red - Numeric formatted 12 // Casted 12

System.ConsoleColor
        Black - Numeric formatted 0 // Casted 0
        DarkBlue - Numeric formatted 1 // Casted 1
        DarkGreen - Numeric formatted 2 // Casted 2
        DarkCyan - Numeric formatted 3 // Casted 3
        DarkRed - Numeric formatted 4 // Casted 4
        DarkMagenta - Numeric formatted 5 // Casted 5
        DarkYellow - Numeric formatted 6 // Casted 6
        Gray - Numeric formatted 7 // Casted 7
        DarkGray - Numeric formatted 8 // Casted 8
        Blue - Numeric formatted 9 // Casted 9
        Green - Numeric formatted 10 // Casted 10
        Cyan - Numeric formatted 11 // Casted 11
        Red - Numeric formatted 12 // Casted 12
        Magenta - Numeric formatted 13 // Casted 13
        Yellow - Numeric formatted 14 // Casted 14
        White - Numeric formatted 15 // Casted 15

> 

Output – Image

enum.color.01.20200209.0730PM

TroubleShooting

Errors

Errors – Cannot convert null to type “System.ConsoleColor” due to enumeration values that are not valid.

Code – Original


[enum] [System.ConsoleColor]$colorWhite=$null;

Output


Cannot convert null to type "System.ConsoleColor" due to enumeration values that are not valid. Specify one of the following enumeration values and try again. 

The possible enumeration values are "Black,DarkBlue,DarkGreen,DarkCyan,DarkRed,DarkMagenta,DarkYellow,Gray,DarkGray,Blue,Green,Cyan,Red,Magenta,Yellow,White".

Remediation – Outline

  1. Define as simply enum and not type specific enum
    • For example [enum][system.ConsoleColor] becomes [enum]
  2. Hardcode valid numeric equivalence
    • [enum] [System.ConsoleColor]$colorWhiteAsNumeric=0;
  3. Hardcode valid enum value
    • [enum] [System.ConsoleColor]$colorWhite=[ConsoleColor]::White;

Remediation – Code



Set-StrictMode -Version 1.0

#[enum] [System.ConsoleColor]$colorWhite=$null;

#Define as generic enum
[enum] $colorWhite=$null;

#Hardcode valid numeric equivalence
[enum] [System.ConsoleColor]$colorWhiteAsNumeric=0;

#Hardcode valid enum value
[enum] [System.ConsoleColor]$colorWhite=[ConsoleColor]::White;

Source Code Control

Gist

Powershell – Enum – Color
Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s