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.
Getting started…
To create the VM information window we need to add a new Windows form to our project.
We’ll call this new form aboutVM.
Layout the form with three labels and two buttons as shown below.
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.
Then create the codeblock for lbVM_DoubleClick
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.
Instead, we’ll need to create a new list of type VirtualMachine.
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!
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.
Now, add the FindAll line to the lbVM_DoubleClick codeblock. We specify that we only want one object returned by using the FirstOrDefault method.
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.
The last thing we need to do here is open the aboutVM form.
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.
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.
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.
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.
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.
Run the application and double-click on a VM.
This should minimize the application and bring up our autos window. If you don’t see this window, go to Debug –> Windows –> Autos.
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.
When you’ve finished, click Continue.
Take a look at the sweet new info form!!
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?
Enter sharedObjects.selectedVM.Runtime.Host.
Run the application and let’s give it a try!
Now, let’s shut down our VM. We’ll use the ShutdownGuest method, but you could use PowerOffVM() for a less gracefull shutdown.
Let’s test the shut down.
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.
Great work! Very helpful examples.
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.
https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/doc/viwin_devg.pdf
Check here for some helpful examples