I’ve created an awful lot of PowerCLI scripts over the years and even created GUI’s for many of them using PowerShell Studio. It’s incredibly gratifying to be able to craft your own tools to make admin life easier while learning something in the process. It’s not a stretch to say development days are my favorite days at work.
Over the past few weeks I’ve been getting back into Visual Studio to write applications that interface with some Arduino projects I’m working on. Those have been fun winter weekend projects, but during the work week I’m a VMware admin, so how do I keep my C sharp? Surely there must be a way to bring the magic of PowerCLI and .NET together. There is…
In this post I’m going to show you how you can create your own vSphere inventory application. We’ll learn how to make connections to vCenter and poll it for VM, Host, and Cluster objects. We’ll do this by leveraging the VMware.VIM Library used by PowerCLI. The application we create will look something like this.
Creating the form
Create a new C# Windows Forms Application and add three list boxes as shown below.
Add the labels, text boxes, and Connect button.
On to the code!
First, our project needs to reference the VMware.VIM namespace which can be found in the directory where PowerCLI is installed.
(C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\VMware.Vim.dll)
Now we need to hook the VMware.Vim namespace in to our code.
Let’s add the System.Collections.Specialized namespace while we’re here. You’ll see why in a bit.
Next we need to declare some lists to dump the data we receive from the vCenter.
Connecting to vCenter
Now that the framework has been built it’s time to start diving into VMware.VIM.
The connection to vCenter is made by calling the Connect and Login methods of the VIMClient class. To use these methods, we need to create a new object called Client. We’ll use this object to connect to the vCenter’s SDK WebService.
In PowerCLI we simply point the Connect-VIServer cmdlet to the vCenter server, but when accessing it via the VIMclient, we need to include the full SDK URL which is simply https://vcenter-server/sdk.
Because we don’t want to connect before the user has entered their vCenter and credential information, we’ll take these actions on the button (btnConnect) click.
Now that we can connect to vCenter, what’s next? How about we pull some VM names? We can do this by calling our client’s FindEntityViews method.
You’re eager to try this out, I can tell! Go ahead!
That’s great! But what if we just wanted to list Windows VM’s? How do we filter for those? This is pretty easy. We just filter for “Windows” against the Config.GuestFullName property.
To do this we need to create the NameValueCollection called filter (this is why we needed to reference the System.Collections.Specialized name space earlier).
Let’s try it now!
It works! My VCSA named VC1 is no longer displayed! If you want to filter against other VM properties, just add another filter.Add line!
Finally, we need to add the host and cluster views by scoping the view to HostSystem, and ClusterComputerResource.
There you have it! You’ve just created your first vSphere tool in C#! If you don’t want to create your own forms and enter the code listed above, feel free to download this solution from GitHub or the new VMware Sample Exchange.