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.
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.
“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.”
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”
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”
$OnLoadFormEvent={ #TODO: Initialize Form Controls here # Create an Array for List of Properties which the user sees $array = @("Bios_Information","Computer_System_Information","Processor_Information") # Appending the list of items in $array to combobox $array | ForEach-Object {Load-ComboBox -ComboBox $combobox1 -Append -Items $_ } }
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.
$buttonResetServerName_Click={ #TODO: Place custom script here $textbox1.Clear() }
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 .
$buttonGo_Click={ #TODO: Place custom script here # Work only if Textbox1.text input is Not Null. if ($textbox1.Text -ne $null) # Selected index is greater than -1 (0,1,2), Iterate for each selected item generate bios info and Out-grid view if ($combobox1.SelectedIndex -gt -1 -and $combobox1.SelectedItem -eq "Bios_Information") { $servername = $textbox1.Text Get-WmiObject -Class win32_bios -ComputerName $servername -ea 'Stop' | Out-GridView -Title "$($combobox1.SelectedItem) for $servername" } # Selected index is greater than -1 (0,1,2), Iterate for each selected item generate bios info and Out-grid view elseif ($combobox1.SelectedIndex -gt -1 -and $combobox1.SelectedItem -eq "Computer_System_Information") { $servername = $textbox1.Text Get-WmiObject -Class Win32_ComputerSystem -ComputerName $servername -ea 'Stop' | Out-GridView -Title "$($combobox1.SelectedItem) for $servername" } <# Selected index is greater than -1 (0,1,2), Iterate for each selected item generate processor info and Out-grid view, error action stop so that we can trap the error in try catch block #> elseif ($combobox1.SelectedIndex -gt -1 -and $combobox1.SelectedItem -eq "Processor_Information") { $servername = $textbox1.Text Get-WmiObject -Class Win32_Processor -ComputerName $servername -ea 'Stop' | Out-GridView -Title "$($combobox1.SelectedItem) for $servername" } } } # Pop up a windows message box indicating the type of error. catch { [void][System.Windows.Forms.MessageBox]::Show(" $($servername) is ShutDown or not Reachable over the Network","Information") }
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
Nice review of GUIs and PowerShell Studio
Thanks.
Right, it is very good IDE, but it is very expensive..
Not really that expensive. You need to get it on a good sale when they occur. Even then, a great tool is worth it's weight in gold.
What do think, which is the best portable PowerShell editor with intellisense?
@Zachary Loeber – I will be waiting for discount for PS Studio 2012.. 🙂
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.
working code available here:
http://poshcode.com/4485
Pingback: SAPIEN Technologies » Blog Archive » PowerShell GUIs: Where do I start?