meanstoanend

The first rule of medicine (for a doctor anyway) is “do no harm”.  It’s been passed down from the early Greeks, and they perfected Yogurt, and Yogurt is supposed to be healthy, so it makes sense.

But in programming, we don’t always follow that seemingly dogmatic rule.  With each new programming language we gain new features, increased brevity (an interesting term), and so on.  But we often cut loose some things in the jump to the next big thing.  In the case of PowerShell, which I really love now, there’s a rather odd way of how getting user input in VBScript kind of got left at the bus stop when the PowerShell bus left.

VBScript offers up InputBox, MsgBox functions, and the Shell.PopUp method.  These are easy to use and fairly simple, compact and reliable, for getting user input and showing users something quick and getting out of the way.

PowerShell v5 still leaves us scrounging a bit for ourselves when it comes to a simple popup form with some buttons, a caption, a message and a symbol.  Yes, the MsgBox (or MessageBox, if you get into the .NET aspects).

Choice = MsgBox(“Continue with this?”, vbYesNo+vbQuestion, “Prompt”)

In PowerShell, you can do this quite a few different ways, but NONE are as compact if you avoid sneaking around through the COM back door.  You can concoct (or adapt, as one of my colleagues jokes) some code to make your own cmdlet/function…

function Show-MessageBox {
 PARAM (
 [parameter(Mandatory=$True, Position=0)] [string] $Caption,
 [parameter(Mandatory=$True, Position=1)] [string] $Message,
 [parameter(Mandatory=$False, Position=2)] 
 [ValidateSet("OkOnly","OkCancel","AbortRetryIgnore","YesNo","YesNoCancel","RetryCancel")] 
 [string] $ButtonSet = "OkOnly",
 [parameter(Mandatory=$False, Position=3)]
 [ValidateSet("None","Information","Question","Exclamation")]
 [string] $IconType = "None"
 )
 switch ($ButtonSet) {
 'RetryCancel' {$buttons = 5; break}
 'YesNo' {$buttons = 4; break}
 'YesNoCancel' {$buttons = 3; break}
 'AbortRetryIgnore' {$buttons = 2; break}
 'OkCancel' {$buttons = 1; break}
 default {$buttons = 0}
 }
 switch ($IconType) {
 'Information' {$icon = 64; break}
 'Exclamation' {$icon = 48; break}
 'Question' {$icon = 32; break}
 'Critical' {$icon = 16; break}
 default {$icon = 0}
 }
 $result = [System.Windows.Forms.MessageBox]::Show($Message, $Caption, $buttons, $icon) 
 return $result
}

There’s also many other alternatives, like this, and this as well.  But still…

The lengthy enumerations and reflection references are kind of odd for 2015 and the age of shortening our journey from thought to product.  Maybe it’s just me.  Then again, when I left LISP and C++ to learn Java and VB, I saw the same thing.  Maybe it’s also a reflection of society? (yes, this is a reach indeed).  How often do you hear older folks comment on how the “new” generation has lost something they had, while at the same time, the “new” generation gains so many new things (usually technology-related).  Trade-offs indeed.

Thoughts?

Advertisements

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 )

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