Search code examples
virtual-machinevsphere

To Get the VM Created Date


I am new to the VMWare Sdk Programming,i have a requirement to get the Virtual Machine (VM) Deployed date.

I have written the below code to get the other required details.

package com.vmware.vim25.mo.samples;

import java.net.URL;

import com.vmware.vim25.*;
import com.vmware.vim25.mo.*; 


public class HelloVM {


     public static void main(String[] args) throws Exception
     {
     long start = System.currentTimeMillis();
     int i;
     ServiceInstance si = new ServiceInstance(new URL("https://bgl-clvs-vc.bgl.com/sdk"), "sbibi", "sibi_123", true);
     long end = System.currentTimeMillis();
     System.out.println("time taken:" + (end-start));
     Folder rootFolder = si.getRootFolder();
     String name = rootFolder.getName();
     System.out.println("root:" + name);
     ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");


     System.out.println("No oF vm:" + mes.length);
     if(mes==null || mes.length ==0)
     {
         return;
     }
     for(i=0;i<mes.length; i++){

         VirtualMachine vm = (VirtualMachine) mes[i];

         VirtualMachineConfigInfo vminfo = vm.getConfig();
         VirtualMachineCapability vmc = vm.getCapability();
         vm.getResourcePool();
         System.out.println("VM Name " + vm.getName());
         System.out.println("GuestOS: " + vminfo.getGuestFullName());
         System.out.println("Multiple snapshot supported: " + vmc.isMultipleSnapshotsSupported());
         System.out.println("Summary: " + vminfo.getDatastoreUrl()); 
     }


     si.getServerConnection().logout();

     } 


}

Can anyone help me how I can get the VM created date?


Solution

  • I have found the Vm Creation Date using the below codes.

    EventFilterSpecByUsername uFilter =
    new EventFilterSpecByUsername();
    uFilter.setSystemUser(false);
    uFilter.setUserList(new String[] {"administrator"});
    Event[] events = evtMgr.queryEvents(efs);
    // print each of the events
    for(int i=0; events!=null && i<events.length; i++)  
    {
          System.out.println("\nEvent #" + i); 
          printEvent(events[i]);
    }
    
    /**
    * Only print an event as Event type.
    */
     static void printEvent(Event evt)
     {
        String typeName = evt.getClass().getName();
        int lastDot = typeName.lastIndexOf('.');
         if(lastDot != -1)
          {
               typeName = typeName.substring(lastDot+1);
           }
               System.out.println("Time:" + evt.getCreatedTime().getTime());
       }
    

    Hope this code might help others.