Search code examples
.netauthenticationvmwarepowercli

Loading a session file with vSphere PowerCLI .net API does not work


I am using vSphere PowerCLI SDK to send some commands via F# and .Net to one of my virtual machines. In order to connect, I was using the vimClient.Login(username, password) function and everything was working fine. Unfortunately, I would like to not have to enter my username and password each time, and not leave it hard coded in the code either.

A solution I have found was to save a session file and load it next time I would like to send commands via F# and .Net, like is explained in the following VMware documentation (bottom of page 14): VMware documentation

Unfortunately, this does not seem to work. The file gets created properly when I call vimClient.SaveSession("VimSession.txt"), but when I vimClient.LoadSession("VimSession.txt") and then try to get Entities with the following line:

let vmViews = vimClient.FindEntityViews(typedefof<VirtualMachine>, null, filterVms, null)
                |> Seq.cast<VirtualMachine>
                |> Seq.toList

I get a null error, thus meaning it did not find any views.

What am I doing wrong here?


Solution

  • I have finally found the problem. I was closing the client's connection, so when I loaded my session file, it did not work as the connection was not left opened, which is logical thinking back on it now.

    Make sure to not call vimClient.Logout() if ever you want to use session files, or else you will run into the same problem as I have. Be careful too as I think there is a timeout on the vSphere server for opened sessions that are inactive too long, so that can become a problem as well (but is configurable).