Home > Blogs > VMware PowerCLI Blog


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

4 thoughts on “PowerShell GUIs

  1. Andrew Tearle

    [, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089] …
    Antonio – Thanx!,
    Can I ask what the above ‘does’ in relation to the Form ? Is it simply defining the appearance ?

    Reply
  2. Antonio Dias

    That’s the “full name” of the Forms assembly. The script loads it to have access to the Forms classes.

    Reply
  3. halr9000

    Antonio, how about a way to refresh the data? I thought it would be cool if $data would accept a scriptblock object. If it is, call it upon building the form initially. Then, include a refresh button that will invoke it again and update the contents of the form.

    Reply
  4. Antonio Dias

    I’ve been waiting for someone to ask for it, Hal. I’m lazy that way. 🙂
    It wouldn’t be hard. I’ll take a look at it when I have time. Or if someone gets to it first, please let us know.
    By the way, thanks for the mention at http://halr9000.com/article/446.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*