Uncategorized

PowerShell GUIs

The PowerShell community has quite a few UI-driven or UI-friendly tools that work nicely with custom scripts. Examples include PowerGUI, PowerGadgets and Workflow Studio.

In that spirit, I sometimes want an ad hoc UI for some of my management activities. The attached script lets me take data and wrap it in a super-simple UI. At the top, I have a multi-column listview showing the data. At the bottom, I can have buttons with scriptblocks attached to them to act on the selected rows. Simple stuff but useful. And quite general-purpose.

Here are a couple of usage examples. I showed a variation on this during my VMworld talk this year:

out-form -title "Disconnect Devices From…"
              -data (get-vm) -columnNames ("VM", "Floppy Count", "CD Count")

              -columnProperties ("Name", "FloppyDrives.Count", "CDDrives.Count")
              -actions @{"Disconnect" = {$_ | get-cddrive | set-cddrive -nomedia

                               -confirm:$false; $_ | get-floppydrive | set-floppydrive -nomedia
                               -confirm:$false;}}

This gives me a list of VMs along with the number of floppy and CD drives for each. I select one or more, push the "Disconnect" button, and the floppy and CD devices are disconnected. With a little PowerShell command-line (and a little bit of scripting backing it), we have a custom application. Now take that command, drop it in a .ps1 file alongside the out-form script and we have a packaged, redistributable application. Cool. :)

Unless you've got an early access copy of our PowerShell cmdlets, you can't try the example above yet. But here's an example you can try:

out-form -title "Services" -data (get-service)
              -columnNames ("Name", "Status")
              -columnProperties ("DisplayName", "Status")

              -actions @{"Start" = {$_.start()}; "Stop" = {$_.stop()};}

  Services_2

This obviously isn’t the most interesting use case. After all, Windows already has a service manager. The interesting uses will come from things you do in your environment. For me, that’s virtualization management, thus the example above. If you think this might be useful, download the script and try wrapping a UI around something you manage. Use it yourself. Hand your newly-created app to a coworker. If you find this useful, I’d love to hear about how you use or improve it.

Here’s the script: 

Download out-form.ps1

Hopefully the parameters are self-explanatory. Here’s a quick summary of each just in case:

1. title – title string to display

2. data – the objects providing the information to render

3. columnProperties – the properties of the data to display, one per column

4. columnNames – name to give each column. If not specified, the property names will be used (columnProperties). If specified, columnNames must be the same size as columnProperties

5. actions – an associative array mapping names to scriptblocks. Each name string will have its own button with the accompanying scriptblock linked to its click event. If not specified, no buttons will appear.

Antonio Dias