3

vSphere .NET application development for PowerCLI users Part II

Welcome back to part 2 of our vSphere .NET application development series. In part 1, we wrote an application that displays the VM’s, Hosts, and Clusters in our vCenter using the VMware.VIM library. Today, we’re going to build upon that application by adding the ability to view information about a VM and change its power state. By adding these features, we’ll gain a valuable understanding of the information and tasks available through this API.

image

Getting started…

To create the VM information window we need to add a new Windows form to our project.

image

 

We’ll call this new form aboutVM.

image

 

Layout the form with three labels and two buttons as shown below.

image

 

Open the code view for our Inventory form so we can create the actions for when the user double clicks on the lbVM listbox.

First, create a new MouseEventHandler for lbVM_DoubleClick. Add this just after the line where we initialize our Inventory form.

image

 

Then create the codeblock for lbVM_DoubleClick

image

 

Now we need to find and extract the virtual machine that our user double clicks on. We’ll do this by using the FindAll method to identify the vm where .Name is the same as the item selected in the lbVM list box. You might think we’d search against vmlist, but the problem is this list is type EntitiyViewBase which does not contain a definition for Name.

image

 

Instead, we’ll need to create a new list of type VirtualMachine.

image

 

To populate our new vmObjects list, we could try casting our EntityViewBase vmlist into vmObjects, OR…

Since each vm in our foreach loop is already of type VirtualMachine, we can simply add each vm into our vmObjects list! It’s so simple it’s ludicrous!

image

 

Moss-Ludicrous

 

 

The hand-off…

There are may ways we can pass objects between our two forms, but the simplest is to create a public class called sharedObjects. Within this class we’ll create a public static VirtualMachine object.

image

 

Now, add the FindAll line to the lbVM_DoubleClick codeblock. We specify that we only want one object returned by using the FirstOrDefault method.

image

 

If we don’t include the FirstOrDefault method, Visual Studio assumes that we want to convert a list to a single object which will break the build.

image

 

The last thing we need to do here is open the aboutVM form.

image

 

Focus on the aboutVM form

We’re done with our inventory form for the moment, so let’s focus on the aboutVM form. Open the code for our aboutVM form and add a reference to the VMware.Vim namespace.

image

 

We want our labels to display something useful such as our VM’s name, the number of CPU’s, and how much memory it has. This is very simple to do since we made the selectedVM object static.

lblName will display the name of the VM by collecting the selectedVM.Name from our sharedObjects class.

image

 

Update the lblCPU and lblMem labels. selectedVM.Config.Hardware.NumCPU will return the number of CPU’s our VM has, and selectedVM.Config.Hardware.MemoryMB will return the amount of memory in MB. Notice we can append text to the returned values as long as we convert them to strings.

image

 

Retrieving the VM properties

You might be wondering how I found out that the CPU configuration is Config.Hardware.NumCPU. Intellisense will give you a pretty good idea, but let’s take a moment and I’ll show you how to use breakpoints to gather this information.

Open the Inventory form’s code and click in the grey column on the left next to line 93 where we create the new aboutform.

image

 

You should now see a giant red dot. This is our breakpoint. This will pause our code before executing this line so we can see what’s happening with our objects.

image

 

Run the application and double-click on a VM.

image

 

This should minimize the application and bring up our autos window. If you don’t see this window, go to Debug –> Windows –> Autos.

image

 

Expand the selectedVM object and take a look around. For example, expand Config and then Hardware to view the number of CPU’s and memory.

image

 

When you’ve finished, click Continue.

image

 

Take a look at the sweet new info form!!

image

 

Close the application and remove the breakpoint by clicking on the red dot next to line 93.

 

Changing the power states.

So far we have a pretty awesome application, but the Power On and Shut Down buttons do nothing.

To power on our VM in PowerCLI, we simply use Start-VM, and similary here, we’ll use the PowerOnVM method. This seems straight forward but what about this ManagedObjectReference host?

image

 

Enter sharedObjects.selectedVM.Runtime.Host.

image

 

Run the application and let’s give it a try!

image

 

image

 

Now, let’s shut down our VM. We’ll use the ShutdownGuest method, but you could use PowerOffVM() for a less gracefull shutdown.

image

 

Let’s test the shut down.

image

 

image

Very nice!!

Through these examples we’ve learned how to use the VMware.VIM library to connect to vCenter and pull objects from its inventory. We’ve also learned how to gather configuration info for our virtual machines and interact with them.

I appreciate all your comments from part one, and I’d love to hear what you plan on building in the comments below.

Matt Bradford

3 Comments

  1. Nice C# examples. I need some help on how to check the VM after being powered on or off. I can’t seem to refresh(Update) the View afterward so I can power cycle the VM again without starting the application over.

    Please contact me, if you want me to send you screenshots in what I’m doing.

Leave a Reply to AnonymousCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.