Reader small image

You're reading from  Mastering PowerCLI

Product typeBook
Published inOct 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781785286858
Edition1st Edition
Languages
Right arrow
Author (1)
Sajal Debnath
Sajal Debnath
author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath

Right arrow

Chapter 11. Creating Windows GUI

So far, we have discussed all aspects of the VMware environment. In the previous two chapters, we discussed how to utilize the vSphere APIs and REST APIs. In this chapter, we are going to see how to build a Windows GUI and create a frontend for your scripts. Yes, I know it seems strange—if you are creating a script, then why would you create a GUI for that. This is particularly useful for building interactive tasks and is a cool feature to have. It can be very useful in other situations also; for example, I worked for an organization where we frequently received a lot of requests to increase the disk size of Linux VMs. The front line support team was not well versed with LVM, so I had to write a GUI tool for them where they could increase the disks without knowing about the backend commands.

We are going to discuss three different ways to build a GUI using PowerShell and cover the following topics:

  • Different ways to create a Windows GUI

  • GUI using Windows Forms...

Different ways to create a Windows GUI


To build a graphical interface in the Windows environment, we can take two approaches: the simpler one is using Windows Forms and the more complex one is using Windows Presentation Foundation (WPF).

The main difference between these two is as follows:

  • Windows Forms: Windows Forms is the name given to the GUI class library included in Microsoft .NET Framework. This is the platform that we can use to build the interface tier for a multi-tier client application. Moreover, an application using Windows Forms is typically an event-driven application supported by the .NET Framework, which spends most of its time waiting for end user inputs in some format. It is basically a graphical API that provides access to native Microsoft Windows interface elements.

  • Windows Presentation Foundation: WPF is a graphical subsystem for rendering user interfaces in Windows-based applications. WPF employs XAML, an XML-based language to define the UI elements and the relationships...

Building a GUI using Windows Forms


The first option that we will discuss is Windows Forms. To utilize Windows Forms, we first need to load the respective assemblies. We can load the assemblies by using the following techniques:

[void][System.Reflection.Assembly]::LoadWithPartialName
("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName
("System.Windows.Forms")

Alternatively, we can use the following technique:

Add-Type -AssemblyNameSystem.Windows.Forms
Add-Type -AssemblyNameSystem.Drawing

Once the assemblies are loaded, we need to load the visual styles method as well. We can load the module by using the following method:

[void] [System.Windows.Forms.Application]::EnableVisualStyles()

I prefer using [void] to add the assemblies because it would suppress unnecessary text output. For the rest of the examples, we will use the following three lines to load the assemblies and the method:

[void][System.Reflection.Assembly]::LoadWithPartialName
("System.Drawing")  
[void][System.Reflection...

Using SAPIEN PowerShell Studio


In the previous section, we discussed how we can utilize WinForms to give a frontend to our scripts. But doing it manually is really painful. To build a simple tool such as the one we have built, we had to write 421 lines of code. We can simplify all these with the use of PowerShell Studio 2015 by SAPIEN. SAPIEN earlier had a free community edition Primal Forms, which has since been discontinued. PowerShell Studio 2015 is a paid version, but if you create a lot of GUIs using WinForms in PowerShell, then this is an invaluable tool for you. It comes with a 45-day trial period so that you can try it before making the final decision. Without wasting much time, let's start the discussion about building a GUI using this tool.

We will start by creating a New Form Project:

We give it the name TextBox_PStudio:

This gives us a blank form:

On the left-hand side, we can see that there is a Toolbox option. There are all kinds of control and control sets available in Toolbox...

Building GUI using WPF


In this section, we are going to discuss how to build a windows GUI using WPF and PowerShell. WPF is more modern and provides a lot more flexibility and advanced options than WinForm. Like WinForm, building a GUI in WPF is also an event-driven approach. We define the Form and then add the controls in the form, after we define the events for the Controls so that we can control the behavior of the Form. The difference is in the way UI elements are designed in WPF. Here, we use XAML-based definitions for the UI elements.

There are two ways in which we can build a WPF-based application, either we hand code the XAML definitions or use third-party tools. The most easily available one is Visual Studio 2015, and it provides a great interface. For our use, I will use Visual Studio 2015 Community Edition (it is a free tool). In Visual Studio, we have two options: the first one is to use the Visual Studio interface and the second option is to use Blend for Visual Studio. Using...

Summary


In this chapter, we discussed how to build a Windows GUI using different methods and give a frontend to our scripts. We saw how we can take help of third-party tools to build our own executable file, which when run will show the form and execute the workflows that we define.

This concludes all the topics that I wanted to cover in the book. In the next chapter, we will talk about PowerShell scripting best practices and discuss some sample scripts.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering PowerCLI
Published in: Oct 2015Publisher: PacktISBN-13: 9781785286858
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath