Display a popup box at cmdlet completion in a script

by MohnJadden at 2013-01-10 08:40:55

Hi all,

I’ve used PS for tasks that would otherwise use dsadd/dsquery in the past, some use of diff to compare CSVs, etc. I’ve been thusly marked the designated PS guy for my IT operations group and with some help, have created a script to get users in OUs, pipe them to an array, and when we click the Add button in a dropdown to which the user’s friendly names are displayed, it runs the following function:

function Add-ToGroup($arg) {

Add-ADGroupMember “CN=WKS_Admins- Win7,OU=Domain Groups,DC=company,DC=companyname,DC=com” $arg

write-host “If you did not see a long red error message, the user was successfully elevated. They will need to reboot to pick up their new rights. If you received an error message, please verify your user and escalate if necessary.” -foregroundcolor green

That write-host is my slapdash means of telling the Help Desk users that all is well by means of bright green text in the PS console window. However, if possible, I’ve been asked to make a friendlier means of success/failure notification.

How do I set a popup box to appear with a success or failure result? I googled and got linked to this, but I don’t understand how to actually make the output happen: http://msdn.microsoft.com/en-us/library … 36(v=vs.85).aspx Am I supposed to do a | and then some kind of cmdlet to call Output)?

I also tried with Sapien Powershell Studio 2012’s demo and in the object browser, I can browse to the System.Management.Automation.Host.PSHostUserInterface.WriteLine or System.Console.WriteLine objects but I have no clue how to utilize them - it looks more like something a .Net developer would use. (No real background in development from my end, unfortunately).

If I build the script as an exe with PS Studio, it’ll take the write-host and display a popup saying the message from above. However if it fails, it’ll pop up an error stating that the user’s already in the group, but it is followed by multiple user-unfriendly windows. In a pinch, I could change the write-host line to say “if you have a bunch of popups before this, the user was not elevated. If this is the only popup, the user was elevated successfully.” That seems a bit clunky, though.

Can anyone help or advise?
by DonJ at 2013-01-10 08:51:36
So… understand that PowerShell isn’t meant to do this. You’re leaving PowerShell proper and entering the .NET Framework world. You’re also eliminating the possibility of using the same function in any non-interactive way, such as scheduling it as a scheduled task. You’re not guaranteeing that the pop-up will be seen unless you code it as Modal, and even then it can be hidden by other application windows (standard OS behavior). Generally speaking, what you’re looking for is considered a poor practice in PowerShell. However, read on.

So assuming you’re okay with all that, what you are probably looking for is http://gallery.technet.microsoft.com/sc … x-982f6906. You could include the entire provided function inside your Add-ToGroup function (not the best command name, by the way… doesn’t follow command naming guidelines; Add-GroupMember would be better since the noun isn’t meant to contain something like “to,” but I’m just being nitpicky!), and then call Show-MsgBox as needed. Note that “Show” is the absolute proper verb for this, because “Show” means "displays an on-screen element intended only for human visual consumption and for no other potential purpose."

Since it sounds like you’re building all of this in a GUI, then the caveats above don’t apply so much. If this is already embedded in a GUI, then showing more GUI is the proper thing to do.
by MohnJadden at 2013-01-11 05:27:41
Wow, from the man who wrote the book that’s been my lunch reading the past week or so - doesn’t get much more authoritative! The book is perfect for me and thanks for taking the time and effort to write it, doubly so for responding so quickly and without malice. I’ve tried working with a PS thread on a broader non-IT forum and got a lot of flames.

This is strictly for manual intervention - we’ll be granting Help Desk people permissions to the security group that governs admin rights and it’ll never be automated.

My question on this - how can I get show-msgbox to pop up only when a cmdlet successfully completes? I don’t mind if it just throws an error if there’s a problem running the cmdlet, but if it runs and throws nothing, I’d like to have it run

show-msgbox -prompt “The user now has admin rights. Please have them reboot to pick up permissions.” -Boxtype OKOnly
by DonJ at 2013-01-11 07:44:06
Well, in PowerShell, no news is good news. If the command cmdlets without an error, it was successful. Use -ErrorVariable to capture any error that occurred, and you can use -ErrorAction and a Trap construct to trap any error that does occur.

$success = $true
Try {
Get-WmiObject -Class Something -Computer Somewhere -EV MyErr -EA Stop
} Catch {
$success = $False
if ($success) { Show-MsgBox etc }

Kinda like that.

BTW… thanks for the kind words :). Not sure which Lunches book you have, but the original one and the new Toolmaking one go into this error handling stuff in some detail.