Today's Friday Fun offers a way for you to graphically interact with your PowerShell scripts and functions without resorting to a lot of complex Winform scripting. I have a function that you can use to display an interactive message box complete with buttons like Yes, No or Cancel. You can either use the message box to display a message to the user or return input.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The function is a wrapper for the MsgBox() method of the [Microsoft.VisualBasic.Interaction] .NET class.
[cc lang="PowerShell"]
Function Get-MessageBox {
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Specify a display message")]
[ValidateNotNullorEmpty()]
[ValidateScript({$_.Length -le 1024})]
[string]$Message,
[ValidateSet("OkOnly","OkCancel","AbortRetryIgnore","YesNoCancel","YesNo","RetryCancel")]
[string]$Button="OkOnly",
[ValidateSet("Critical", "Question", "Exclamation", "Information")]
[string]$Icon="Information",
[string]$Title,
[switch]$Passthru
)
#load the required .NET assembly
Add-Type -AssemblyName "Microsoft.VisualBasic"
#Invoke the message box and save the value from any button clicks to a variable
$returnValue=[Microsoft.Visualbasic.Interaction]::Msgbox($message,"$button,$icon",$title)
#write return value if -Passthru is called
if ($passthru)
{
Write-Output $returnValue
}
} #end function
[/cc]
The method allows you to specify the message text (up to 1024 characters), an icon, a button set and a title. The only value you really need to specify is a message. The default icon is Information and the default title is blank. The function uses the ValidateSet attribute to verify that the right value is specified for icon and title.
When you run this function, the user has to click on one of the buttons. The return value is stored in a variable.
[cc lang="PowerShell"]
$returnValue=[Microsoft.Visualbasic.Interaction]::Msgbox($message,"$button,$icon",$title)
[/cc]
I wrote the function assuming that most of the time all you want is to present an informational message with an OK button which means you don't really need to see the return value. Thus the default behavior is NOT to write it to the pipeline. However, if you use -Passthru, then the value will be written to the pipeline. This allows you to use code like:
[cc lang="PowerShell"]
$rc=Get-Messagebox -message "Do you know what you're doing?" -icon "exclamation" -button "YesNoCancel" -title "Hey $env:username!!" -passthru
Switch ($rc) {
"Yes" {"I hope your resume is up to date."}
"No" {"Wise move."}
"cancel" {"When in doubt, punt."}
Default {"nothing returned"}
}
[/cc]
Here's the actual message box.
The message parameter value must be a string and you can create multi-line entries using `n.
[cc lang="PowerShell"]
PS C:\> $m="Hello $env:username `n It is now $(Get-Date)"
PS C:\> Get-MessageBox -Message $m -Title "Greeting"
[/cc]
The script file with the function also includes an optional alias, gmd. You can download the script file here. The full script also has comment-based help. Enjoy.