dblock
Enthusiast
Enthusiast

VMWareTasks: A VIX API C# Library (let's reinvent less wheels)

I posted a CodeProject Article this morning with a VMWareTasks C# library on top of the VIX API. It exposes a better programming model for the 99% of those synchronous VMWare scenarios where you don't want to deal with jobs or other more complex constructs. I think we can all benefit from reimplementing the same thing less often!

I'd love to get more features/patches for the library, feel free to (re)use and contribute.

cheers

dB.

Update: I posted a new build 1.0.1604.0:

0 Kudos
25 Replies
eatorres
Enthusiast
Enthusiast

Good job! I have also written a wrapper for Vix API, but not nearly as thorough as yours.

0 Kudos
admin
Immortal
Immortal

Daniel, great stuff, I've posted an example of using it from PowerShell.

I noticed one strange thing, when I ran notepad like in your example it didn't return until I killed the process. Is that a VIX-side thing or something in your library?

0 Kudos
dblock
Enthusiast
Enthusiast

Thanks! The library is synchronous, so Execute runs notepad and returns only when it's done. My example might have been not so clever - if you run a GUI application without a remote desktop you're not going to be able to do much Smiley Happy

0 Kudos
JPatten
Enthusiast
Enthusiast

dB - Thanks for this. It is going to make using VIX a lot easier now. I was able to use it against Workstation on my machine and a remote Server 2.0 machine. I tried it against ESXi 3.5, but was unable to connect. I think it has to do with the ESX server not being at 3.5 U3 per the VIX documents, so I will have to wait and see.

Carter - Thanks for the PowerShell example. I usually try to keep all of my programming inside of PowerShell and you gave me a good start.

0 Kudos
admin
Immortal
Immortal

It should work against U2 and up, I'm not sure if the notes say U3.

Anyway, VIX will connect even against ESX 3.0, it's just that nothing you do after that will actually work. Make sure you're connecting to https://server/sdk rather than just the server name or IP.

0 Kudos
JPatten
Enthusiast
Enthusiast

Here is the code block I am using that works for server 2.0. I am able to connect to the ESXi 3.5 server via VI API and PowerShell, but not with VIX

add-type -path " Vestris.VMWareLib.dll"

add-type -path “Interop.VixCOM.dll"

$myHost= new-object vestris.vmwarelib.vmwarevirtualhost

$myHost.ConnectToVMWareVIServer("https://<IP>/sdk",0, “<login>”, “<pw>”)

$vm = $myHost.Open(<VMX Location>)

$vm.Login(“<login>”, “<pw>”)

$vm.Execute(“notepad.exe”,$null)

In Workstation and Server, Notepad will launch. With ESX, I get a failed to connect to host message.

0 Kudos
akoeplinger
Enthusiast
Enthusiast

Wow, looks like we were working on the same thing the last weeks, since I published my VIX API C# library to Codeplex one day after you wrote your CodeProject article :smileygrin:

My library basically does the same as yours, it simplifies dealing with the VIX API and makes common tasks (powering on/off, copying files) a lot easier.

I also took the time and fully commented the functions (using the official documentation), so you get hints from IntelliSense while coding, which I find really helpful.

Although your library implements less functions than mine (I wrapped them all...), it seems like you have the better concept when dealing with IJobs.

If you want to take a look at my approach, visit and download the library Smiley Wink

Regards, Alex

0 Kudos
dblock
Enthusiast
Enthusiast

Hey Alex,

I've been adding features while you were away! I really think the VMWare development team should produce something like this, so hopefuly seeing multiple implementations will give them the right object model to start with. A library that is not a wrapper on top of the COM API is really the right answer, there's no need to have a COM object registered to talk to VMWare. You wouldn't need to package the VIX COM DLLs if you were to distribute anything that talks to VMWare. Heck, VMWare should give us a million bucks to share for designing it for them Smiley Happy

If I may allow some criticism of the stuff on CodePlex: I think that your library needs to be more consistent with constructs like snapshots or shared folders: those are all objects and should be manipulated as collections. I chose not to implement methods like AddSharedFolder(name, description) - from the object model perspective you want VirtualMachine.SharedFolders.Add(new VMWareSharedFolder(name, description)), then VirtualMachine.SharedFolders reflects the changes. This also applies to snapshots and all other things that belong to the VM. Other important aspects include an active timeout - VMWare won't timeout in blocking operations.

I also wanted to thank you - I learned something from your implementation - I was wondering what the right way of implementing things like Snapshot.Name is - your code helped. I also added Snapshot.Path - post]]].

Some competition is a good thing! People should use whatever they think is best. Of course, you're welcome to give up the CodePlex library and contribute to VMWareTasks Smiley Wink

cheers

dB.

0 Kudos
akoeplinger
Enthusiast
Enthusiast

Hi!

I totally agree that an implementation on top of the COM API shouldn't be necessary, cause this just adds another layer of complexity. It would also be nice if there was a way to use our library on Linux systems (e.g. by leveraging the capabilites of the Mono framework).

Of course I also looked at your source-code and I have to admit that your implementation follows the concept of OOP much better Smiley Wink

My intention was to just have a library that looks similar to the original VIX API, but uses C#-style parameters and return values and hides me from dealing with handles, blocking, etc.

I'll have to look at implementing a timeout for the Wait() function, thanks for that !

As you've said, we can only benefit from having seperate approaches and maybe the VMware developement team benefits too Smiley Happy

Regards, Alex

0 Kudos
JPatten
Enthusiast
Enthusiast

I have now tried both COM Wrappers and receive the same error when trying to connect to my ESXi host. This leads me to believe that something is missing on the host itself, since it works on my Server 2.0 and Workstation installs.

I have now tested against two different ESXi 3.5 hosts and receive the error "Unable to Connect to Host". Is there anything I need to check on the host? Does VIX have to be installed locally on the host or is it on by default?

Thanks

0 Kudos
akoeplinger
Enthusiast
Enthusiast

Hi!

I just tried it against my ESXi host (3.5.0 build-123629) and it works without problems. Remember that you need at least ESX Update 2 for VIX to work, the default port for connection is 443. Basically, VIX needs the same credentials/IP that you would enter when you connect with the VMware Infrastructure Client.

You don't need to install VIX on the host, just on the machine from where you want to connect to the ESXi server.

If that doesn't help, please let me know Smiley Wink

Regards, Alex

0 Kudos
JPatten
Enthusiast
Enthusiast

I think I am going to wait for the server admin to come back after the new year and apply at least U2. The server I am connecting to is ESXi 3.5 110271. I am using root to login, so that shouldn't be the problem.

I'll try after I get the server updated and let you know the results.

Thanks

0 Kudos
eatorres
Enthusiast
Enthusiast

Hi Alex,

I tried downloading the documentation file for your wrapper, but it did not seem to want to open... I was wondering if your wrapper supports copying files located in a guest VM to and from a datastore on an ESX 3.5 server. I have been researching the VIX documentation and the various VI API documentation and can't seem to get a definitive answer whether or not this is possible. I have tried various ways of inputting the path to the file on the datastore (for example "\[datastore1\] folder/folder/file.extension" as well as "/vmfs/volumes/(long hex string)/folder/folder/file.extension") but nothing seems to work. Do you have any experience with copying files to and from datastores? Any help would be greatly appreciated!

0 Kudos
akoeplinger
Enthusiast
Enthusiast

Hi!

The documentation is a standard CHM-helpfile, it should open on any Windows system since Windows 98 Smiley Happy There may be problems if you try to open it from a network location, so store it locally on your computer.

To answer your question: the wrapper doesn't support copying files directly from a ESX datastore to a guest VM and vice versa. There are functions that copy files (CopyFileFromHostToGuest and CopyFileFromGuestToHost) but they copy only to/from the system where the Vix client is running, not the ESX host itself.

You could copy the file to your local machine first, and then upload the file to the datastore (which should be possible with the VI API, I guess).

Regards, Alex

0 Kudos
JPatten
Enthusiast
Enthusiast

If you are running Windows Vista, the CHM file will not open properly until you "Unblock" it. Right click on the file and choose properties. There will be and "Unblock" button at the bottom.

Alex - Similar to what Carter did with dB's wrapper, I have managed to use your wrapper in powershell, mainly because you have the screen print feature. Using it in powershell is definately easier than just using the basic VIX in C#. Thanks again.

0 Kudos
akoeplinger
Enthusiast
Enthusiast

If you are running Windows Vista, the CHM file will not open properly until you "Unblock" it.

Ah, yes, I forgot about that Smiley Wink

Glad to hear that my work is useful for you, if you have any suggestions for further improvements, please let me know!

Regards, Alex

0 Kudos
JPatten
Enthusiast
Enthusiast

Not sure why, but I now tried to connect using the IP address instead of the host name and I was able to connect. When I ping the host name, it comes back with the correct IP. Oh well.

Thanks

0 Kudos
dblock
Enthusiast
Enthusiast

This might be hiding a problem. The host has to be listening on the correct IP (or all IPs if you have multiple NICs) and accept a request for a given host name. The https connection might be a problem too. I would try to navigate to , and see what the error, if any, is.

0 Kudos
fixitchris
Hot Shot
Hot Shot

Very, very sleek implementations guys! Thanks!

I found that connecting to a hostname does not work either.

Private Function getLocalIP() As String

Dim localHost As String

Dim IPHEntry As IPHostEntry

Dim IPAdd() As IPAddress

localHost = Dns.GetHostName()

IPHEntry = Dns.GetHostEntry(localHost)

IPAdd = IPHEntry.AddressList

Try

For i = 0 To IPAdd.GetUpperBound(0)

If IPAdd(i).AddressFamily = Sockets.AddressFamily.InterNetwork Then

Return IPAdd(i).ToString

End If

Next

Catch ex As Exception

Return "127.0.0.1"

End Try

End Function

dblock, I have a couple questions for you:

1. How did you determine your timeouts? I see that a timeout for copying a file is 20 minutes, remove a snap is 10 minutes.

2. You mention that you still implement synchronous WAITs, and it looks like you just do a CHECK COMPLETION on each job and time out if necessary. Is there any advantage to doing callbacks instead?

3. Take a look at my question and implementation of VIX, I would appreciate any feedback: http://communities.vmware.com/thread/187966?tstart=0

0 Kudos