The best tool to interact programmatically with VI3 is the SDK. The SDK is very powerful and its power can be harnessed with another equally powerful tool, .NET. In this tip, I will describe how to leverage the VI3 SDK with .NET using C#.
Before you read this article be sure to check out part one in this series.
Before you can use the VI3 SDK with C#, you need to generate C# class files from the VI3 Web Services Description Language (WSDL) files. Although Microsoft Visual Studio .NET will perform this task automatically when you add a Web reference, this crashed my IDE. I believe the reason for this is that the class file generated from vim.wsdl is almost 2 MB in size in .NET 2.0 and 820 KB in size in .NET 1.1.
Your best bet is to download the SDK's WSDL file and save the files to disk.
You can also download the SDK from VMware (you will need to register with VMware to download the SDK). Be sure to download the VI3 SDK package and not the CIM or Guest SDK, because the VI3 SDK contains the Getting Started and Programming guides as well as an extensive collection of HTML files on the SDK object model.
However you obtain the WSDL files, you should end up with two files, vim.wsdl and vimservice.wsdl. VMware is aware of a bug ( with some potential solutions) that causes up to two-minute load times for programs that reference the .NET 2.0 generated VI3 class files because of poor XML serialization. I highly recommend that you generate your VI3 class files using the .NET 1.1 WSDL tool.
Use the .NET tool wsdl.exe to generate C# classes from these files. The syntax for this is:
This command will produce the file VimApi.cs ...
To continue reading for free, register below or login
To read more you must become a member of SearchVMware.com
');
// -->

that contains the C# class representation of the WSDL files under the namespace "VimApi." You should replace the path to the WSDL binary with whatever is appropriate for your system, but you should make very sure that you are using the correct version of the binary.
If your machine has both .NET 1.1 and .NET 2.0 installed, then most likely the .NET 2.0 binary will be what gets executed if you simply type "wsdl" in a command prompt (if you have added the relevant paths to your system or user PATH environment variable).
Although the .NET 1.1 class file will work in .NET 2.0, the class file generated with the .NET 2.0 version of wsdl.exe will not work in .NET 1.1 because the .NET 2.0 generated files include code exclusive to .NET 2.0, such as partial classes. It is important to know which version of the WSDL binary you are using.
Congratulations, you are ready to start coding your first C# program with the VI3 SDK! And I already *have* your first C# program that uses the VI3 SDK right here beside me!
The name of the program is "hostshow." The program is a small, simple binary file that when executed, shows all of the hosts accessible by the VI3 SDK. We will look at the parts of the code that interact with the VI3 SDK line-by-line.
You can download the code and other files we will be looking at here from my Website. Decompress the zip file once it has been downloaded so that we can examine the included files:
- - hostshow.cs (This is the main source file.)
- - make_all.bat (This batch file builds a .NET 1.1 binary and a .NET
2.0 binary from the source.)
- - make_dotnet_1.1.bat (This batch file builds a .NET 1.1 binary from the source.)
- - make_dotnet_2.0.bat (This batch file builds a .NET 2.0 binary from the source.)
- - vim.wsdl (This is the same vim.wsdl that comes with the VI3 SDK.)
- - vimservice.wsdl (This is the same vimservice.wsdl that comes with the VI3 SDK.)
- - VimApi_dotnet_1.1.cs (This is the C# source file generated from
vim.wsdl and vimservice.wsdl using the .NET 1.1 wsdl.exe tool.)
- - VimApi_dotnet_2.0.cs (This is the C# source file generated from
vim.wsdl and vimservice.wsdl using the .NET 2.0 wsdl.exe tool.)
So right about now you may be thinking to yourself, "Why did he make me go through all of the trouble of building the class file VimApi.cs if he was going to give it to me?!?" The answer is practice, pure and simple. The more comfortable you are with VI3, C# and their related tools, the more adept you be at writing programs that leverage them.
Although the file "make_dotnet_2.0.bat" uses the .NET 2.0 version of the VimApi file, it is possible to use the .NET 1.1 version of the VimApi file with .NET 2.0 code. In fact, doing so will speed up .NET 2.0 applications that use the VI3 SDK to almost the same speeds as their .NET 1.1 counterparts.
I did not do it this way because I wanted to keep things as straightforward as possible when explaining how to build a C# program that uses the VI3 SDK, but I wanted you to know that it can be done.
There is more code in hostshow.cs than we are going to look at in this article, but the code we are not looking at does not interact with the VI3 SDK; rather it is the code necessary to provide a useful command line application.
I am sure most people reading this article have their own methods of writing command line apps, and my way is certainly no better than yours. So use mine, use yours. It does not matter. What does matter is how we make our programs talk to Virtual Infrastructure 3, so turn to line ~57 in hostshow.cs and let us begin.
try
article I wrote on VI3 and certificates.
finally
I hope that this small application is simple enough and that I explained it with sufficient detail and clarity so that you can use it as a building block to designing your own applications with the VI3 SDK.
In this article, I demonstrated how to query the VI3 SDK for information, such as the available hosts. Stay tuned for part three of this series, where I will describe how to alter your VI3 configuration with the VI3 SDK and C#!
About the author: Andrew Kutz is deeply embedded in the dark, dangerous world of virtualization. Andrew is an avid fan of .NET, open source, Terminal Services, coding and comics. He is a Microsoft Certified Solutions Developer (MCSD) and a SANS/GIAC Certified Windows Security Administrator (GCWN). Andrew graduated from the University of Texas at Austin with a BA in Ancient History and Classical Civilization and currently lives in Austin, Tex., with his wife Mandy and their two puppies, Lucy and CJ.