2013/10/06

PowerShell Studio 2012 - WinForms - Creating a basic GUI (Video)

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

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



Thanks for Reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter: @lazywinadm

4 comments:

  1. I wish I had 349 bucks after reading your post and watching the video! That tool rocks!
    Btw, I always forget changing the executionpolicy on new servers too! :-)

    ReplyDelete
    Replies
    1. Thank you Carlo!
      Well I made this video just after finishing the rebuild of my workstation on Windows 8.1
      That's Why! ;-D

      Delete
  2. I tried it and it sure rocks.
    Can you please list down the things you did to correct the compilation errors the first time.
    Those are the set scripts you ran on PS which are hard to read on the video.

    Thank you!

    ReplyDelete
    Replies
    1. Thanks for the comment Amador! Appreciated!

      The issue I got at the end of the video was related to the ExecutionPolicy. By default it is set to RESTRICTED and I set it to REMOTESIGNED. It's a Security restriction related to PowerShell not an compilation issue.

      To fix it: Open PowerShell (Run as Administrator) and then enter: Set-ExecutionPolicy RemoteSigned, answer YES to the question. This should do it ;-)

      Fx

      Delete