Building GUI for PowerShell Scripts using PowerShell Studio 2012

Building GUI for PowerShell Scripts using PowerShell Studio 2012

Building GUI for PowerShell Scripts

Building GUI for PowerShell Scripts – Why a GUI ?A GUI can often make a task easier and more constrained for less experienced users for example our “Service Desk” Folks.PowerShell is built on the .NET framework which has entire set of capabilities for building GUI’s.

These capabilities are accessible to powershell but require quite lengthy and complicated coding.

To avoid this complications i recommend using a Nice Tool from Sapien, PowerShell Studio 2012.

PowerShell Studio gives you the tools you need to bring your PowerShell tasks to successful
completion.

Building GUI for PowerShell Scripts
 PowerShell Studio Enables you to package your script into executables (.exe) and share it with others who have relatively less scripting experience.

You can download the free trial from http://www.sapien.com/software/powershell_studio

After the Trial Expires you can extend the functionality by procuring a license key from sapien.

You can also use  Primal Forms Community Edition, No Export to Executable option in Primal Forms Community Edition.

However You can export to ps1 and integrate it in your script 🙂

PowerShell
Studio 2012 – Key
Features

  • PowerShell Console – Not just one, but two hot-swappable consoles: 32-bit and 64-bit
  • PowerShell Script Editor – Awesome Script Editor, But now PowerShell v3-v4 script editor also turn out be a good option.
  • PowerShell Forms Designer -An easy Drag and Drop way of creating PowerShell Scripts, saves lots of time and error by doing it the manual way.
Don’t expect that You Cannot build a New Version of Microsoft Word or Excel with this Tool :), Complicated Multi-Window applications are possible but very complex, Stick with Simple Forms for collecting and displaying data, also don’t forget
Out-GridView – An easy way to turn output to graphical view.

“Lets
See a Practical Example, We
Will create a very simple Tool to extract System Properties for a Server.

 

This tool when run would as the user to input a server name and based on the server name it would display its BIOS,Computer System and Processor Information.”

Here a look at the Final Tool and also its .exe file (with a custom icon).

So let me illustrate the steps on how you can build this nice tool

1) Open PowerShell Studio 2012 and Click on New, New Form Project, Feed in the Project Name in my example its “System Information” and click on Create.

 

You would see a similar screen as below, Double click on “Main Form.pff”

 2) Next  we need to add controls to the main form, Click on Object Browser on the left pane and append the appropriate control objects to the form, below i have shown a screen cap of which all controls need to be appended from the object browser.
Building GUI for PowerShell Scripts

Once you append the controls to the MainForm, next you need to change certain properties for the control objects.

Here is a list of changes which you would need to make on each control.

  • Label – Change the text property to “Server Name” and “Select System Property”.
  • ComboBox – Change the Drop down style to Dropdown list so that users cannot manipulate its values.
  • Button – Change the text property to “Reset Server Name” and “Go!!”.
  • MainForm –  Change the text property to “System Information”
Next we will start with the actual coding, Double click on the Form in GUI which would open up the Script view of the form $OnloadFormEvent. Inside it we would initialize the Array of objects and also add the same to the combobox.

 

 

Next we again go back to the designer view and double click on the “Reset Server Name” Button which would take us to its “Click” event.When a user clicks on this button it would call a clear method on textbox1 which would result in clearing its contents.

 

Next we again go back to the designer view and double click on the “Go!!” Button which would take us to its “Click” event.

This is what the script is all about, When a user types in the server name in textbox and selects one of the system property of his interests and clicks this button an Out-Grid view form would pop up which would show a detailed report for the system for that particular system property .

 Also note i have illustrated a small sample of how we can use the Try/catch block to trap errors.For example if the user types in a wrong system name which is not reachable over network or is shutdown an appropriate warning error message box would pop up.
 Also note that with the PowerShell Studio Snippet view has a message box snippet which when dragged into the script drops in a scriptblock for “MessageBox” without the need for any additional coding.
 
Here’s the Script which we need to type in to the “Go” Button Event.

Once you are set with the scripting part, Hit “Ctrl + F4” or the “Run Project Button” on the Home Ribbon to test out the tool

Once you feel that the script and the UI work as per your requirement, Package and convert the tool to an exe using the ‘Package Button” located on the Export Ribbon

Here i have illustrated a small demo of how the Final Execution of the Tool Would Look.

Here i enter the server name and from the dropdown box i select BIOS information and Click on Go!!, an Out-Grid window would pop up with the details

 

Similarly we would test out the same for CPU information by selecting CPUInformation from drop down box
Nice, so you can see our tool works perfectly fine.
Next lets take the last use case where in i enter a systemname which doesnot exists or is not reachable over network, here our try catch block would kick in and throw an error message that the system does not exists or is not reachable over network
Note that you can dig more into the try-catch block and trap each unique error with its unique catch block.
I hope this blog post on “Building GUI for PowerShell Scripts” helps you in paving the stepping stone for you PowerShell Script GUI’s.
Please follow and like us:

Share this post

9 thoughts on “Building GUI for PowerShell Scripts using PowerShell Studio 2012

  1. Keep getting this error when running your script:

    The term 'Load-Combobox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    Reply

Post Comment