using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Text; using System.Threading.Tasks; namespace WebsitePanel.Providers.Virtualization { public static class HardDriveHelper { public static VirtualHardDiskInfo[] Get(PowerShellManager powerShell, string vmname) { List disks = new List(); Collection result = GetPS(powerShell, vmname); if (result != null && result.Count > 0) { foreach (PSObject d in result) { VirtualHardDiskInfo disk = new VirtualHardDiskInfo(); disk.SupportPersistentReservations = Convert.ToBoolean(d.GetProperty("SupportPersistentReservations")); disk.MaximumIOPS = Convert.ToUInt64(d.GetProperty("MaximumIOPS")); disk.MinimumIOPS = Convert.ToUInt64(d.GetProperty("MinimumIOPS")); disk.VHDControllerType = d.GetEnum("ControllerType"); disk.ControllerNumber = Convert.ToInt32(d.GetProperty("ControllerNumber")); disk.ControllerLocation = Convert.ToInt32(d.GetProperty("ControllerLocation")); disk.Path = d.GetProperty("Path").ToString(); disk.Name = d.GetProperty("Name").ToString(); GetVirtualHardDiskDetail(powerShell, disk.Path, ref disk); disks.Add(disk); } } return disks.ToArray(); } //public static VirtualHardDiskInfo GetByPath(PowerShellManager powerShell, string vhdPath) //{ // VirtualHardDiskInfo info = null; // var vmNames = new List(); // Command cmd = new Command("Get-VM"); // Collection result = powerShell.Execute(cmd, true); // if (result == null || result.Count == 0) // return null; // vmNames = result.Select(r => r.GetString("Name")).ToList(); // var drives = vmNames.SelectMany(n => Get(powerShell, n)); // return drives.FirstOrDefault(d=>d.Path == vhdPath); //} public static Collection GetPS(PowerShellManager powerShell, string vmname) { Command cmd = new Command("Get-VMHardDiskDrive"); cmd.Parameters.Add("VMName", vmname); return powerShell.Execute(cmd, true); } public static void GetVirtualHardDiskDetail(PowerShellManager powerShell, string path, ref VirtualHardDiskInfo disk) { if (!string.IsNullOrEmpty(path)) { Command cmd = new Command("Get-VHD"); cmd.Parameters.Add("Path", path); Collection result = powerShell.Execute(cmd, true); if (result != null && result.Count > 0) { disk.DiskFormat = result[0].GetEnum("VhdFormat"); disk.DiskType = result[0].GetEnum("VhdType"); disk.ParentPath = result[0].GetProperty("ParentPath"); disk.MaxInternalSize = Convert.ToInt64(result[0].GetProperty("Size")); disk.FileSize = Convert.ToInt64(result[0].GetProperty("FileSize")); disk.Attached = disk.InUse = Convert.ToBoolean(result[0].GetProperty("Attached")); } } } } }