Over the past few weeks many users have begun to e-mail me asking for not just help with the VI SDK, but code examples as well. I am more than happy to help, but I want to be able to help as many people as I can. To that end I have decided to post all of my code examples online for everyone’s benefit. You can access these examples at http://www.lostcreations.com/code/wiki/vmware. Examples will generally be available in C#, Java, and VIPerl. If you have an idea for an example, or a language you would like to see it in, please let me know!
The first example that I have posted is how to log into and out of the VI SDK Web service. There are examples in C#, VI Perl, and Java. With Java though there is a catch. See, you cannot actually successfully logout of the VI Web service with Java — it will throw an exception. This is a known bug according to some of VMware’s own code examples — they say that the logout function is not yet implemented.
However, this is not acceptable for anyone writing, oh, I don’t know, an OS-agnostic version of the VirtualInfrastructure client in Java. There has to be a way to force the session to log off. Well there is, but before I tell you what it is I will tell you the methods I first attempted at that failed:
The first thing that I tried was to use the TerminateSession method and terminate my own session. Unfortunately terminating your own session is prohibited (I *could* on the other hand terminate someone else’s).
The second hair-brained scheme I came up with was using the CreateScheduledTask method to invoke a script on the remote server that would terminate my session given my session ID. Unfortunately I was simply not able to get this to work. I tried it in C#, Java, and VI Perl, but I could not get a task created with action type RunScriptAction. Please let me know if you can get this to work.
The method that finally worked involves TerminateSession in VI Perl. Using a VI Perl script I wrote called killsession.pl I terminate my own session by invoking said Perl script from within the Java example. It isn’t pretty, but it works. And until VMware fixes their stuff, it will have to do.
Hope this helps!