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.
The screenshot above is produced with the following test function:
[code language=”powershell”]
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”
}
}
[/code]
Producing the GUI is as simple as this:
[code language=”powershell”]
Import-Module “$PATHTOMODULE\Show-CommandGUI.psm1”
Show-CommandGUI test
[/code]
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.
Photo Credit: Fernando X. Sanchez via Compfight cc