public void SyncInventory(object state)
        {
            TraceFactory.Logger.Debug($"Synchronizing VM inventory with {_vCenterUri.ToString()}.");

            List <VSphereVirtualMachine> serverVMs = new List <VSphereVirtualMachine>();

            using (var vSphereController = new VSphereVMController(_vCenterUri, _credential))
            {
                serverVMs.AddRange(vSphereController.GetVirtualMachines());
            }
            TraceFactory.Logger.Debug($"Synchronizing {serverVMs.Count} virtual machines.");

            int updated = 0;

            using (AssetInventoryContext context = new AssetInventoryContext(_connectionString))
            {
                List <FrameworkClient> inventoryVMs = context.FrameworkClients.ToList();
                foreach (FrameworkClient inventoryVM in inventoryVMs)
                {
                    VSphereVirtualMachine serverVM = serverVMs.FirstOrDefault(n => n.HostName == inventoryVM.FrameworkClientHostName);
                    if (serverVM != null)
                    {
                        VMUsageState currentState = EnumUtil.GetByDescription <VMUsageState>(inventoryVM.UsageState);

                        if (currentState != VMUsageState.Unavailable && currentState != VMUsageState.DoNotSchedule && string.IsNullOrEmpty(inventoryVM.SessionId))
                        {
                            inventoryVM.PowerState  = EnumUtil.GetDescription(GetPowerState(serverVM));
                            inventoryVM.UsageState  = EnumUtil.GetDescription(GetUsageState(serverVM));
                            inventoryVM.LastUpdated = DateTime.Now;
                            updated++;
                        }
                    }
                }

                context.SaveChanges();
            }

            TraceFactory.Logger.Debug($"Synchronization complete. Updated {updated} virtual machines.");
        }
Ejemplo n.º 2
0
 private static VSphereVirtualMachine GetVirtualMachine(VSphereVMController vSphereController, string hostName)
 {
     return(vSphereController.GetVirtualMachines().First(n => n.HostName.Equals(hostName, StringComparison.OrdinalIgnoreCase)));
 }