The following post will demo how to create a basic Graphical User Interface with SAPIEN PowerShell Studio 2012.
Update:See also the second part of this post:
PowerShell Studio 2012 - WinForms - GUI ToolMaking
Last year I released a PowerShell script called
LazyWinAdmin 0.4 which is a script that generate a Graphical User Interface. I used SAPIEN PowerShell Studio 2012 to create this Interface and write my PowerShell code.
LazyWinAdmin script allows SysAdmins/IT Pros to Query Information on their Workstation / Servers and to Perform some actions like :
* List Shares (with local path), Processes, Services, ...
* Test the Connection, Permission, PowerShell Remoting, RDP availability ...
* Reboot/Shutdown
* Query and Kill any RDP Session opened
* Etc...
Since then, I got a lot of emails asking me questions about PowerShell and how to make GUIs using PowerShell Studio 2012. So here is a quick demo..
|
LazyWinAdmin v0.4 Script |
###
Overview
* Creating a basic GUI -
Video
* Creating a basic GUI -
Step by Step
* Get PowerShell Studio 2012
* Creating the GUI
* Editing the GUI
* Adding the PowerShell Code
* Download
The following post will demo how to create a basic Graphical User Interface with SAPIEN PowerShell Studio 2012.
We will simply add a couple of controls (Buttons and a Richtextbox), and add a few events to write in the Richtextbox when pressing the buttons.
###
Get PowerShell Studio 2012
I think PowerShell Studio is the most powerful and feature complete PowerShell Integrated Scripting Environment (ISE) available today. Of course you can create and edit normal PowerShell scripts but you can also create single and multiple forms GUI and export them as PS1 or EXE files !
Some of my favorite features are the Function explorer and the Snippets explorer.
As today this product will cost you $349 USD.
[http://www.sapien.com/software/powershell_studio](http://www.sapien.com/software/powershell_studio)
Sapien also has a free version:
PrimalForms Community Edition (PrimalForms CE), but this tool will only allow you to create the user interface and export it to a PowerShell file. You can not directly write PowerShell code for each actions you wish your script to perform.You will have to do this manually once the file is exported from PrimalForms CE.
###
Creating the GUI
Select
File /
New /
New Form
|
|
In the
Form Template selection, select the
Empty Form.
###
Editing the GUI
Drag and drop the controls into the main form. Here I added 3
Buttons and 1
RichTextBox
Arrange the Form so it does not look too weird :-)
Select one of the button and edit the property TEXT to change the name of it.
Repeat this step for each buttons
###
Adding the PowerShell code
Select a button and do one of the following action to add the powershell code :
Double Click on the button
OR
Double click on the Event
|
For Example, here I added the code highlighted for the "Add Red Text" Button. |
PowerShell code for each buttons. I only used the event "Click".
```
$buttonClear_Click={
# Clear the RichTextBox Control
$richtextbox1.Clear()
}
$buttonAddRedText_Click={
# Change the RichTextBox Color before adding the new text
$richtextbox1.SelectionColor = 'Red'
# Adding some text with a return (to go to the next line)
$richtextbox1.AppendText("Red Text`r")
}
$buttonAddBlueText_Click={
# Change the RichTextBox Color before adding the new text
$richtextbox1.SelectionColor = 'blue'
# Adding some text with a return (to go to the next line)
$richtextbox1.AppendText("Blue Text`r")
}
```
|
Final GUI ! :-) |
###
DOWNLOAD
Technet Script Repository
Leave a comment