/// <summary> /// Gets the VM Power state. /// </summary> /// <param name="virtualMachine">The vm.</param> /// <returns></returns> private static VMPowerState GetPowerState(VSphereVirtualMachine virtualMachine) { if (virtualMachine != null && virtualMachine.PowerState == VirtualMachinePowerState.PoweredOn) { return(VMPowerState.PoweredOn); } return(VMPowerState.PoweredOff); }
/// <summary> /// Gets the state of the VM usage based on the power state of the VM. /// </summary> /// <param name="virtualMachine">The vm.</param> /// <returns></returns> private static VMUsageState GetUsageState(VSphereVirtualMachine virtualMachine) { if (virtualMachine != null && virtualMachine.PowerState == VirtualMachinePowerState.PoweredOn) { return(VMUsageState.InUse); } return(VMUsageState.Available); }
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."); }