/// <summary>
 /// The main entry point for the application.
 /// </summary>
 public static void Main(String[] args)
 {
     try
     {
         PropertyCollector app = new PropertyCollector();
         cb = AppUtil.AppUtil.initialize("PropertyCollector",
                                 PropertyCollector.constructOptions(),
                                 args);
         cb.connect();
         ManagedObjectReference sic = cb.getConnection().ServiceRef;
         _sic = cb.getConnection()._sic;
         _service = cb.getConnection()._service;
         String dcName = cb.get_option("dcName");
         String vmDnsName = cb.get_option("vmDnsName");
         ObjectContent[] ocs = null;
         ManagedObjectReference dcMoRef
            = cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
         if (dcMoRef == null)
         {
             Console.WriteLine("Datacenter not found");
         }
         else
         {
             ManagedObjectReference vmMoRef =
                _service.FindByDnsName(_sic.searchIndex,
                                      dcMoRef, vmDnsName, true);
             if (vmMoRef == null)
             {
                 Console.WriteLine("The virtual machine with DNS '" + vmDnsName
                    + "' not found ");
             }
             else
             {
                 // Retrieve name and powerState from a Virtual Machine
                 Object[] properties
                     = getProperties(vmMoRef, new String[] { "name", "runtime.powerState" });
                 String vmName = (String)properties[0];
                 VirtualMachinePowerState vmState
                       = (VirtualMachinePowerState)properties[1];
                 if (vmName != null && vmState != null)
                 {
                     Console.WriteLine("The VM with DNS name \'" + vmDnsName
                                       + "\' is named \'" + vmName +
                                       "\' and is " + vmState.ToString());
                 }
                 ocs = getDatacenters();
                 printObjectContent(ocs, "All Datacenters");
                 // Find all the VMs in the Datacenter
                 ocs = getVMs(dcMoRef);
                 printObjectContent(ocs, "All VMs in the Datacenter: " + dcName);
                 //Find all the Hosts in the Datacenter
                 ocs = getHosts(dcMoRef);
                 printObjectContent(ocs, "All Hosts in the Datacenter: " + dcName);
                 // Display summary information about a VM
                 ocs = getVMInfo(vmMoRef);
                 printVmInfo(ocs);
                 // Display all of inventory
                 ocs = getInventory();
                 printInventoryTree(ocs);
                 ocs = getNetworkInfo(dcMoRef);
                 printNetworkInfo(ocs);
                 cb.disConnect();
                 Console.WriteLine("Press any key to exit:");
                 Console.Read();
             }
         }
     }
     catch (SoapException e)
     {
         if (e.Detail.FirstChild.LocalName.Equals("DuplicateNameFault"))
         {
             Console.WriteLine("Managed Entity with the name already exists");
         }
         else if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
         {
             Console.WriteLine("Specification is invalid");
         }
         else if (e.Detail.FirstChild.LocalName.Equals("InvalidNameFault"))
         {
             Console.WriteLine("Managed Entity Name is empty or too long");
         }
         else if (e.Detail.FirstChild.LocalName.Equals("RuntimeFault"))
         {
             Console.WriteLine(e.Message.ToString() + "Either parent name or item name is invalid");
         }
         else if (e.Detail.FirstChild.LocalName.Equals("RuntimeFault"))
         {
             Console.WriteLine(e.Message.ToString() + " "
                                 + "The Operation is not supported on this object");
         }
         else
         {
             Console.WriteLine(e.Message.ToString() + " "
                              + "The Operation is not supported on this object");
         }
     }
     Console.Read();
 }
        public static void Main(String[] args)
        {
            string sHost = "";
            int totalCpuUsage = 0;
            int totalMemUsage = 0;
            string content="";
            FileStream fs = null;
            if(!File.Exists("../../Output.txt"))
            {
                 fs = new FileStream("../../Output.txt",FileMode.Create);
            }
            else
            {
                 fs = new FileStream("../../Output.txt",FileMode.Append);
            }

            try
            {
                PropertyCollector app = new PropertyCollector();
                cb = AppUtil.AppUtil.initialize("PropertyCollector", PropertyCollector.constructOptions(), args);
                cb.connect();
                ManagedObjectReference sic = cb.getConnection().ServiceRef;
                _sic = cb.getConnection()._sic;
                _service = cb.getConnection()._service;
                String dcName = cb.get_option("dcName");
                String vmName = cb.get_option("vmName");
                ObjectContent[] ocs = null;
                ManagedObjectReference dcMoRef = cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
                if (dcMoRef == null)
                {
                    Console.WriteLine("Datacenter not found");
                }
                else
                {
                    ManagedObjectReference vmMoRef = cb.getServiceUtil().GetDecendentMoRef(null, "VirtualMachine", vmName);
                    if (vmMoRef == null)
                    {
                        Console.WriteLine("The virtual machine with DNS '" + vmName + "' not found ");
                    }
                    else
                    {
                        // Display summary information about a VM
                        while(true)
                        {
                            for (int times = 0; times < 12; times++)
                            {
                                ocs = getVMInfo(vmMoRef);
                                printVmInfo(ocs, ref totalCpuUsage, ref totalMemUsage, ref content, fs, ref sHost);
                                Console.WriteLine("\n The system will halt for 15 seconds \n");
                                Thread.Sleep(15 * 1000);
                            }
                            if ((double)(totalCpuUsage / 120) > 50 || (double)(totalMemUsage) / 12 > 300)
                            {
                                try
                                {
                                    string vmname = "jwang74_2";
                                    string pool = "jwang74";
                                    string[] tHostArray = { "128.230.96.39", "128.230.96.41", "128.230.96.39", "128.230.96.40", "128.230.96.111" };
                                    string tHost = "";
                                    do
                                    {
                                        Random a = new Random();
                                        int i = a.Next(0, 5);
                                        tHost = tHostArray[i];
                                    } while (tHost == sHost);
                                    migrateVM(vmname, pool, tHost, sHost);
                                    Console.WriteLine("Do you want to continue: Y/N");
                                    string con = Console.ReadLine();
                                    if (con.Equals("Y") || con.Equals("y"))
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                catch (Exception)
                                {
                                }
                                Console.WriteLine("Press any key to exit: ");
                                Console.Read();
                            }
                            Console.WriteLine("Do you want to continue: Y/N");
                            string condition = Console.ReadLine();
                            if (condition.Equals("Y") || condition.Equals("y"))
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }

                        cb.disConnect();
                        Console.WriteLine("Press any key to exit:");
                        Console.Read();
                    }
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("DuplicateNameFault"))
                {
                    Console.WriteLine("Managed Entity with the name already exists");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
                {
                    Console.WriteLine("Specification is invalid");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("InvalidNameFault"))
                {
                    Console.WriteLine("Managed Entity Name is empty or too long");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("RuntimeFault"))
                {
                    Console.WriteLine(e.Message.ToString() + "Either parent name or item name is invalid");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("RuntimeFault"))
                {
                    Console.WriteLine(e.Message.ToString() + " " + "The Operation is not supported on this object");
                }
                else
                {
                    Console.WriteLine(e.Message.ToString() + " " + "The Operation is not supported on this object");
                }
            }
            Console.Read();
            fs.Close();
        }