Show-CommandGUI an enhanced Show-Command for PowerShell

2036545266_e1f19d8cdd_m
After my last post (PowerShell tricks – Use Show-Command to add a simple GUI to your functions). I was thinking how one could write a function that would not have the deficiencies that Show-Command has when it comes to providing a GUI for functions. In addition to what Show-Command does I wanted a function that:

  • Considers advanced function parameters (i.e. ValidateScript, ValidatePattern, ValidateRange, ValideLength)
  • Populates the fields with the respective default values (if applicable)
  • Provides a browse for file or browse for folder option (if the parameter name contains ‘file’ or ‘folder’)
  • Provides meaningful error messages via message boxes
  • Doesn’t require the use of Invoke-Expression in order to run the parameters against the command
  • Can be customized to my own preferences

Say hello to Show-CommandGUI.

Show-CommandGUI
The screenshot above is produced with the following test function:

function test{
    [CmdletBinding(DefaultParameterSetName='Basic')]
    Param
    (
        [Parameter(Mandatory=$true, 
                   Position=0,
                   ParameterSetName='Basic')]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("sun", "moon", "earth")] 
        [string]$choice,
        [ValidateRange(2,5)]
        $number,
        [ValidatePattern("^[a-z]")]
        [ValidateLength(2,5)]
        [String]$pattern,
        [ValidateScript({$_ -like '*test'})]
        $string,
        [Parameter(ParameterSetName='Advanced')]
        [switch]$switch,
        [Parameter(Mandatory=$true, ParameterSetName='Advanced')]
        $filePath = 'c:\test.txt',
        [Parameter(Mandatory=$true, ParameterSetName='Basic')]
        $folderPath = 'c:\'
    )
    "pattern: $pattern"
    "number: $number"
    "string: $string"
    if ($PSCmdlet.ParameterSetName -eq 'Advanced'){
        "switch: $switch"
        "filePath: $filePath"
    }
    else{
        "choice: $choice"
        "folderPath: $folderPath"
    }
}

Producing the GUI is as simple as this:

Import-Module "$PATHTOMODULE\Show-CommandGUI.psm1"
Show-CommandGUI test

The module and the test function can be downloaded via GitHub. Please let me know if you have any questions or ideas on how to improve the function.

shareThoughts


Photo Credit: Fernando X. Sanchez via Compfight cc

Advertisements

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s