public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool dvdDriveShouldBeInstalled)
 {
     if (!vm.DvdDriveInstalled && dvdDriveShouldBeInstalled)
         Add(powerShell, vm.Name);
     else if (vm.DvdDriveInstalled && !dvdDriveShouldBeInstalled)
         Remove(powerShell, vm.Name);
 }
        public static VirtualHardDiskInfo[] Get(PowerShellManager powerShell, string vmname)
        {
            List <VirtualHardDiskInfo> disks = new List <VirtualHardDiskInfo>();

            Collection <PSObject> 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>("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 void Update(PowerShellManager powerShell, VirtualMachine vm, bool bootFromCD, bool numLockEnabled)
        {
            // for Win2012R2+ and Win8.1+
            if (vm.Generation == 2)
            {
                Command cmd = new Command("Set-VMFirmware");

                cmd.Parameters.Add("VMName", vm.Name);
                if (bootFromCD)
                    cmd.Parameters.Add("FirstBootDevice", DvdDriveHelper.GetPS(powerShell, vm.Name));
                else
                    cmd.Parameters.Add("FirstBootDevice", HardDriveHelper.GetPS(powerShell, vm.Name).FirstOrDefault());

                powerShell.Execute(cmd, true);
            }
            // for others win and linux
            else
            {
                Command cmd = new Command("Set-VMBios");

                cmd.Parameters.Add("VMName", vm.Name);
                var bootOrder = bootFromCD
                    ? new[] { "CD", "IDE", "LegacyNetworkAdapter", "Floppy" }
                    : new[] { "IDE", "CD", "LegacyNetworkAdapter", "Floppy" };
                cmd.Parameters.Add("StartupOrder", bootOrder);

                powerShell.Execute(cmd, true);
            }
        }
Exemple #4
0
        public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool bootFromCD, bool numLockEnabled)
        {
            // for Win2012R2+ and Win8.1+
            if (vm.Generation == 2)
            {
                Command cmd = new Command("Set-VMFirmware");

                cmd.Parameters.Add("VMName", vm.Name);
                if (bootFromCD)
                {
                    cmd.Parameters.Add("FirstBootDevice", DvdDriveHelper.GetPS(powerShell, vm.Name));
                }
                else
                {
                    cmd.Parameters.Add("FirstBootDevice", HardDriveHelper.GetPS(powerShell, vm.Name).FirstOrDefault());
                }

                powerShell.Execute(cmd, true);
            }
            // for others win and linux
            else
            {
                Command cmd = new Command("Set-VMBios");

                cmd.Parameters.Add("VMName", vm.Name);
                var bootOrder = bootFromCD
                    ? new[] { "CD", "IDE", "LegacyNetworkAdapter", "Floppy" }
                    : new[] { "IDE", "CD", "LegacyNetworkAdapter", "Floppy" };
                cmd.Parameters.Add("StartupOrder", bootOrder);

                powerShell.Execute(cmd, true);
            }
        }
        public static void Update(PowerShellManager powerShell, VirtualMachine vm)
        {
            // External NIC
            if (!vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress))
            {
                Delete(powerShell, vm.Name, vm.ExternalNicMacAddress);
                vm.ExternalNicMacAddress = null; // reset MAC
            }
            else if (vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress)
                && Get(powerShell,vm.Name,vm.ExternalNicMacAddress) == null)
            {
                Add(powerShell, vm.Name, vm.ExternalSwitchId, vm.ExternalNicMacAddress, Constants.EXTERNAL_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter);
            }

            // Private NIC
            if (!vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress))
            {
                Delete(powerShell, vm.Name, vm.PrivateNicMacAddress);
                vm.PrivateNicMacAddress = null; // reset MAC
            }
            else if (vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress)
                 && Get(powerShell, vm.Name, vm.PrivateNicMacAddress) == null)
            {
                Add(powerShell, vm.Name, vm.PrivateSwitchId, vm.PrivateNicMacAddress, Constants.PRIVATE_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter);
            }
        }
Exemple #6
0
        public static VirtualMachineNetworkAdapter[] Get(PowerShellManager powerShell, string vmName)
        {
            List <VirtualMachineNetworkAdapter> adapters = new List <VirtualMachineNetworkAdapter>();

            Command cmd = new Command("Get-VMNetworkAdapter");

            if (!string.IsNullOrEmpty(vmName))
            {
                cmd.Parameters.Add("VMName", vmName);
            }

            Collection <PSObject> result = powerShell.Execute(cmd, true);

            if (result != null && result.Count > 0)
            {
                foreach (PSObject psAdapter in result)
                {
                    VirtualMachineNetworkAdapter adapter = new VirtualMachineNetworkAdapter();

                    adapter.Name       = psAdapter.GetString("Name");
                    adapter.MacAddress = psAdapter.GetString("MacAddress");
                    adapter.SwitchName = psAdapter.GetString("SwitchName");

                    adapters.Add(adapter);
                }
            }
            return(adapters.ToArray());
        }
 public static void UpdateIOPS(PowerShellManager powerShell, VirtualMachine vm, int IOPSmin, int IOPSmax)
 {
     if (vm.Disks != null && (IOPSmax >= IOPSmin))
     {
         foreach (VirtualHardDiskInfo d in vm.Disks)
         {
             Command cmd = new Command("Set-VMHardDiskDrive");
             cmd.Parameters.Add("VMName", vm.Name);
             cmd.Parameters.Add("ControllerType", d.VHDControllerType.ToString());
             cmd.Parameters.Add("ControllerNumber", d.ControllerNumber);
             cmd.Parameters.Add("ControllerLocation", d.ControllerLocation);
             if (IOPSmin == IOPSmax) //disable QoS control
             {
                 cmd.Parameters.Add("MinimumIOPS", false);
                 cmd.Parameters.Add("MaximumIOPS", false);
             }
             else
             {
                 cmd.Parameters.Add("MinimumIOPS", IOPSmin);
                 cmd.Parameters.Add("MaximumIOPS", IOPSmax);
             }
             powerShell.Execute(cmd, true, true);
         }
     }
 }
Exemple #8
0
        public static void Update(PowerShellManager powerShell, VirtualMachine vm)
        {
            // External NIC
            if (!vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress))
            {
                Delete(powerShell, vm.Name, vm.ExternalNicMacAddress);
                vm.ExternalNicMacAddress = null; // reset MAC
            }
            else if (vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress) &&
                     Get(powerShell, vm.Name, vm.ExternalNicMacAddress) == null)
            {
                Add(powerShell, vm.Name, vm.ExternalSwitchId, vm.ExternalNicMacAddress, Constants.EXTERNAL_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter);
            }

            // Private NIC
            if (!vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress))
            {
                Delete(powerShell, vm.Name, vm.PrivateNicMacAddress);
                vm.PrivateNicMacAddress = null; // reset MAC
            }
            else if (vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress) &&
                     Get(powerShell, vm.Name, vm.PrivateNicMacAddress) == null)
            {
                Add(powerShell, vm.Name, vm.PrivateSwitchId, vm.PrivateNicMacAddress, Constants.PRIVATE_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter);
            }
        }
        public static VirtualHardDiskInfo[] Get(PowerShellManager powerShell, string vmname)
        {
            List<VirtualHardDiskInfo> disks = new List<VirtualHardDiskInfo>();

            Collection<PSObject> 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>("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<string>();

        //    Command cmd = new Command("Get-VM");

        //    Collection<PSObject> 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<PSObject> GetPS(PowerShellManager powerShell, string vmname)
        {
            Command cmd = new Command("Get-VMHardDiskDrive");
            cmd.Parameters.Add("VMName", vmname);

            return powerShell.Execute(cmd, true);
        }
Exemple #11
0
        public static void SetReplicaServer(PowerShellManager powerShell, bool enabled, string remoteServer, string thumbprint, string storagePath)
        {
            Command cmd = new Command("Set-VMReplicationServer");

            cmd.Parameters.Add("ReplicationEnabled", enabled);

            if (!string.IsNullOrEmpty(remoteServer))
            {
                cmd.Parameters.Add("ComputerName", remoteServer);
            }

            if (!string.IsNullOrEmpty(thumbprint))
            {
                cmd.Parameters.Add("AllowedAuthenticationType", "Certificate");
                cmd.Parameters.Add("CertificateThumbprint", thumbprint);
            }

            if (!string.IsNullOrEmpty(storagePath))
            {
                cmd.Parameters.Add("ReplicationAllowedFromAnyServer", true);
                cmd.Parameters.Add("DefaultStorageLocation", storagePath);
            }

            powerShell.Execute(cmd, false);
        }
        public static void SetFirewallRule(PowerShellManager powerShell, bool enabled)
        {
            Command cmd = new Command("Enable-Netfirewallrule");
            cmd.Parameters.Add("DisplayName", "Hyper-V Replica HTTPS Listener (TCP-In)");

            powerShell.Execute(cmd, false);
        }
 public static void Delete(PowerShellManager powerShell, string vmName, string server)
 {
     Command cmd = new Command("Remove-VM");
     cmd.Parameters.Add("Name", vmName);
     if (!string.IsNullOrEmpty(server)) cmd.Parameters.Add("ComputerName", server);
     cmd.Parameters.Add("Force");
     powerShell.Execute(cmd, false, true);
 }
Exemple #14
0
        public static void Add(PowerShellManager powerShell, string vmName)
        {
            Command cmd = new Command("Add-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);

            powerShell.Execute(cmd, true);
        }
Exemple #15
0
        public static void SetFirewallRule(PowerShellManager powerShell, bool enabled)
        {
            Command cmd = new Command("Enable-Netfirewallrule");

            cmd.Parameters.Add("DisplayName", "Hyper-V Replica HTTPS Listener (TCP-In)");

            powerShell.Execute(cmd, false);
        }
        public static void RemoveVmReplication(PowerShellManager powerShell, string vmName, string server)
        {
            Command cmd = new Command("Remove-VMReplication");
            cmd.Parameters.Add("VmName", vmName);
            if (!string.IsNullOrEmpty(server)) cmd.Parameters.Add("ComputerName", server);

            powerShell.Execute(cmd, false);
        }
        //public static VirtualHardDiskInfo GetByPath(PowerShellManager powerShell, string vhdPath)
        //{
        //    VirtualHardDiskInfo info = null;
        //    var vmNames = new List<string>();

        //    Command cmd = new Command("Get-VM");

        //    Collection<PSObject> 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 <PSObject> GetPS(PowerShellManager powerShell, string vmname)
        {
            Command cmd = new Command("Get-VMHardDiskDrive");

            cmd.Parameters.Add("VMName", vmname);

            return(powerShell.Execute(cmd, true));
        }
        public static void Add(PowerShellManager powerShell, string vmName)
        {
            Command cmd = new Command("Add-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);

            powerShell.Execute(cmd, true);
        }
Exemple #19
0
        public static void Delete(PowerShellManager powerShell, string vmName, VirtualMachineNetworkAdapter networkAdapter)
        {
            Command cmd = new Command("Remove-VMNetworkAdapter");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("Name", networkAdapter.Name);

            powerShell.Execute(cmd, true);
        }
        public static void Delete(PowerShellManager powerShell, VirtualMachineSnapshot snapshot, bool includeChilds)
        {
            Command cmd = new Command("Remove-VMSnapshot");
            cmd.Parameters.Add("VMName", snapshot.VMName);
            cmd.Parameters.Add("Name", snapshot.Name);
            if (includeChilds) cmd.Parameters.Add("IncludeAllChildSnapshots", true);

            powerShell.Execute(cmd, true);
        }
        public static void Stop(PowerShellManager powerShell, string vmName, bool force, string server)
        {
            Command cmd = new Command("Stop-VM");

            cmd.Parameters.Add("Name", vmName);
            if (force) cmd.Parameters.Add("Force");
            if (!string.IsNullOrEmpty(server)) cmd.Parameters.Add("ComputerName", server);
            //if (!string.IsNullOrEmpty(reason)) cmd.Parameters.Add("Reason", reason);

            powerShell.Execute(cmd, false);
        }
Exemple #22
0
        public static void Delete(PowerShellManager powerShell, string vmName, string macAddress)
        {
            var networkAdapter = Get(powerShell, vmName, macAddress);

            if (networkAdapter == null)
            {
                return;
            }

            Delete(powerShell, vmName, networkAdapter);
        }
Exemple #23
0
 public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool dvdDriveShouldBeInstalled)
 {
     if (!vm.DvdDriveInstalled && dvdDriveShouldBeInstalled)
     {
         Add(powerShell, vm.Name);
     }
     else if (vm.DvdDriveInstalled && !dvdDriveShouldBeInstalled)
     {
         Remove(powerShell, vm.Name);
     }
 }
Exemple #24
0
        public static void RemoveVmReplication(PowerShellManager powerShell, string vmName, string server)
        {
            Command cmd = new Command("Remove-VMReplication");

            cmd.Parameters.Add("VmName", vmName);
            if (!string.IsNullOrEmpty(server))
            {
                cmd.Parameters.Add("ComputerName", server);
            }

            powerShell.Execute(cmd, false);
        }
Exemple #25
0
        public static void UpdateProcessors(PowerShellManager powerShell, VirtualMachine vm, int cpuCores, int cpuLimitSettings, int cpuReserveSettings, int cpuWeightSettings)
        {
            Command cmd = new Command("Set-VMProcessor");

            cmd.Parameters.Add("VMName", vm.Name);
            cmd.Parameters.Add("Count", cpuCores);
            cmd.Parameters.Add("Maximum", cpuLimitSettings);
            cmd.Parameters.Add("Reserve", cpuReserveSettings);
            cmd.Parameters.Add("RelativeWeight", cpuWeightSettings);

            powerShell.Execute(cmd, true);
        }
        public static void Remove(PowerShellManager powerShell, string vmName)
        {
            var dvd = Get(powerShell, vmName);

            Command cmd = new Command("Remove-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("ControllerNumber", dvd.ControllerNumber);
            cmd.Parameters.Add("ControllerLocation", dvd.ControllerLocation);

            powerShell.Execute(cmd, true);
        }
Exemple #27
0
        public static void Remove(PowerShellManager powerShell, string vmName)
        {
            var dvd = Get(powerShell, vmName);

            Command cmd = new Command("Remove-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("ControllerNumber", dvd.ControllerNumber);
            cmd.Parameters.Add("ControllerLocation", dvd.ControllerLocation);

            powerShell.Execute(cmd, true);
        }
        public static void UpdateProcessors(PowerShellManager powerShell, VirtualMachine vm, int cpuCores, int cpuLimitSettings, int cpuReserveSettings, int cpuWeightSettings)
        {
            Command cmd = new Command("Set-VMProcessor");

            cmd.Parameters.Add("VMName", vm.Name);
            cmd.Parameters.Add("Count", cpuCores);
            cmd.Parameters.Add("Maximum", cpuLimitSettings);
            cmd.Parameters.Add("Reserve", cpuReserveSettings);
            cmd.Parameters.Add("RelativeWeight", cpuWeightSettings);

            powerShell.Execute(cmd, true);
        }
Exemple #29
0
        public static void Delete(PowerShellManager powerShell, string vmName, string server)
        {
            Command cmd = new Command("Remove-VM");

            cmd.Parameters.Add("Name", vmName);
            if (!string.IsNullOrEmpty(server))
            {
                cmd.Parameters.Add("ComputerName", server);
            }
            cmd.Parameters.Add("Force");
            powerShell.Execute(cmd, false, true);
        }
Exemple #30
0
        public static void Delete(PowerShellManager powerShell, VirtualMachineSnapshot snapshot, bool includeChilds)
        {
            Command cmd = new Command("Remove-VMSnapshot");

            cmd.Parameters.Add("VMName", snapshot.VMName);
            cmd.Parameters.Add("Name", snapshot.Name);
            if (includeChilds)
            {
                cmd.Parameters.Add("IncludeAllChildSnapshots", true);
            }

            powerShell.Execute(cmd, true);
        }
        public static void Add(PowerShellManager powerShell, string vmName, string switchId, string macAddress, string adapterName, bool legacyAdapter)
        {
            Command cmd = new Command("Add-VMNetworkAdapter");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("Name", adapterName);
            cmd.Parameters.Add("SwitchName", switchId);

            if (String.IsNullOrEmpty(macAddress))
                cmd.Parameters.Add("DynamicMacAddress");
            else
                cmd.Parameters.Add("StaticMacAddress", macAddress);

            powerShell.Execute(cmd, true);
        }
        public static PSObject GetPS(PowerShellManager powerShell, string vmName)
        {
            Command cmd = new Command("Get-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);

            Collection<PSObject> result = powerShell.Execute(cmd, true);

            if (result != null && result.Count > 0)
            {
                return result[0];
            }
            
            return null;
        }
Exemple #33
0
        public static PSObject GetPS(PowerShellManager powerShell, string vmName)
        {
            Command cmd = new Command("Get-VMDvdDrive");

            cmd.Parameters.Add("VMName", vmName);

            Collection <PSObject> result = powerShell.Execute(cmd, true);

            if (result != null && result.Count > 0)
            {
                return(result[0]);
            }

            return(null);
        }
        public static int GetVMProcessors(PowerShellManager powerShell, string name)
        {
            int procs = 0;

            Command cmd = new Command("Get-VMProcessor");

            cmd.Parameters.Add("VMName", name);

            Collection<PSObject> result = powerShell.Execute(cmd, true);
            if (result != null && result.Count > 0)
            {
                procs = Convert.ToInt32(result[0].GetProperty("Count"));

            }
            return procs;
        }
Exemple #35
0
        public static int GetVMProcessors(PowerShellManager powerShell, string name)
        {
            int procs = 0;

            Command cmd = new Command("Get-VMProcessor");

            cmd.Parameters.Add("VMName", name);

            Collection <PSObject> result = powerShell.Execute(cmd, true);

            if (result != null && result.Count > 0)
            {
                procs = Convert.ToInt32(result[0].GetProperty("Count"));
            }
            return(procs);
        }
Exemple #36
0
        public static void Stop(PowerShellManager powerShell, string vmName, bool force, string server)
        {
            Command cmd = new Command("Stop-VM");

            cmd.Parameters.Add("Name", vmName);
            if (force)
            {
                cmd.Parameters.Add("Force");
            }
            if (!string.IsNullOrEmpty(server))
            {
                cmd.Parameters.Add("ComputerName", server);
            }
            //if (!string.IsNullOrEmpty(reason)) cmd.Parameters.Add("Reason", reason);

            powerShell.Execute(cmd, false);
        }
        public static DvdDriveInfo Get(PowerShellManager powerShell, string vmName)
        {
            DvdDriveInfo info = null;

            PSObject result = GetPS(powerShell, vmName);

            if (result != null)
            {
                info = new DvdDriveInfo();
                info.Id = result.GetString("Id");
                info.Name = result.GetString("Name");
                info.ControllerType = result.GetEnum<ControllerType>("ControllerType");
                info.ControllerNumber = result.GetInt("ControllerNumber");
                info.ControllerLocation = result.GetInt("ControllerLocation");
                info.Path = result.GetString("Path");
            }
            return info;
        }
 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<PSObject> result = powerShell.Execute(cmd, true);
         if (result != null && result.Count > 0)
         {
             disk.DiskFormat = result[0].GetEnum<VirtualHardDiskFormat>("VhdFormat");
             disk.DiskType = result[0].GetEnum<VirtualHardDiskType>("VhdType");
             disk.ParentPath = result[0].GetProperty<string>("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"));
         }
     }
 }
 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 <PSObject> result = powerShell.Execute(cmd, true);
         if (result != null && result.Count > 0)
         {
             disk.DiskFormat      = result[0].GetEnum <VirtualHardDiskFormat>("VhdFormat");
             disk.DiskType        = result[0].GetEnum <VirtualHardDiskType>("VhdType");
             disk.ParentPath      = result[0].GetProperty <string>("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"));
         }
     }
 }
Exemple #40
0
        public static DvdDriveInfo Get(PowerShellManager powerShell, string vmName)
        {
            DvdDriveInfo info = null;

            PSObject result = GetPS(powerShell, vmName);

            if (result != null)
            {
                info                    = new DvdDriveInfo();
                info.Id                 = result.GetString("Id");
                info.Name               = result.GetString("Name");
                info.ControllerType     = result.GetEnum <ControllerType>("ControllerType");
                info.ControllerNumber   = result.GetInt("ControllerNumber");
                info.ControllerLocation = result.GetInt("ControllerLocation");
                info.Path               = result.GetString("Path");
            }
            return(info);
        }
        public static OperationalStatus GetVMHeartBeatStatus(PowerShellManager powerShell, string name)
        {
            OperationalStatus status = OperationalStatus.None;

            Command cmd = new Command("Get-VMIntegrationService");

            cmd.Parameters.Add("VMName", name);
            cmd.Parameters.Add("Name", "HeartBeat");

            Collection<PSObject> result = powerShell.Execute(cmd, true);
            if (result != null && result.Count > 0)
            {
                var statusString = result[0].GetProperty("PrimaryOperationalStatus");

                if (statusString != null)
                    status = (OperationalStatus)Enum.Parse(typeof(OperationalStatus), statusString.ToString());
            }
            return status;
        }
Exemple #42
0
        public static void Add(PowerShellManager powerShell, string vmName, string switchId, string macAddress, string adapterName, bool legacyAdapter)
        {
            Command cmd = new Command("Add-VMNetworkAdapter");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("Name", adapterName);
            cmd.Parameters.Add("SwitchName", switchId);

            if (String.IsNullOrEmpty(macAddress))
            {
                cmd.Parameters.Add("DynamicMacAddress");
            }
            else
            {
                cmd.Parameters.Add("StaticMacAddress", macAddress);
            }

            powerShell.Execute(cmd, true);
        }
        public static DynamicMemory GetDynamicMemory(PowerShellManager powerShell, string vmName)
        {
            DynamicMemory info = null;

            Command cmd = new Command("Get-VMMemory");
            cmd.Parameters.Add("VMName", vmName);
            Collection<PSObject> result = powerShell.Execute(cmd);

            if (result != null && result.Count > 0)
            {
                info = new DynamicMemory();
                info.Enabled = result[0].GetBool("DynamicMemoryEnabled");
                info.Minimum = Convert.ToInt32(result[0].GetLong("Minimum") / Constants.Size1M);
                info.Maximum = Convert.ToInt32(result[0].GetLong("Maximum") / Constants.Size1M);
                info.Buffer = Convert.ToInt32(result[0].GetInt("Buffer"));
                info.Priority = Convert.ToInt32(result[0].GetInt("Priority"));
            }

            return info;
        }
Exemple #44
0
        public static DynamicMemory GetDynamicMemory(PowerShellManager powerShell, string vmName)
        {
            DynamicMemory info = null;

            Command cmd = new Command("Get-VMMemory");

            cmd.Parameters.Add("VMName", vmName);
            Collection <PSObject> result = powerShell.Execute(cmd);

            if (result != null && result.Count > 0)
            {
                info          = new DynamicMemory();
                info.Enabled  = result[0].GetBool("DynamicMemoryEnabled");
                info.Minimum  = Convert.ToInt32(result[0].GetLong("Minimum") / Constants.Size1M);
                info.Maximum  = Convert.ToInt32(result[0].GetLong("Maximum") / Constants.Size1M);
                info.Buffer   = Convert.ToInt32(result[0].GetInt("Buffer"));
                info.Priority = Convert.ToInt32(result[0].GetInt("Priority"));
            }

            return(info);
        }
Exemple #45
0
        public static OperationalStatus GetVMHeartBeatStatus(PowerShellManager powerShell, string name)
        {
            OperationalStatus status = OperationalStatus.None;

            Command cmd = new Command("Get-VMIntegrationService");

            cmd.Parameters.Add("VMName", name);
            cmd.Parameters.Add("Name", "HeartBeat");

            Collection <PSObject> result = powerShell.Execute(cmd, true);

            if (result != null && result.Count > 0)
            {
                var statusString = result[0].GetProperty("PrimaryOperationalStatus");

                if (statusString != null)
                {
                    status = (OperationalStatus)Enum.Parse(typeof(OperationalStatus), statusString.ToString());
                }
            }
            return(status);
        }
Exemple #46
0
        public static void Update(PowerShellManager powerShell, VirtualMachine vm, int ramMb, DynamicMemory dynamicMemory)
        {
            Command cmd = new Command("Set-VMMemory");

            cmd.Parameters.Add("VMName", vm.Name);
            cmd.Parameters.Add("StartupBytes", ramMb * Constants.Size1M);

            if (dynamicMemory != null && dynamicMemory.Enabled)
            {
                cmd.Parameters.Add("DynamicMemoryEnabled", true);
                cmd.Parameters.Add("MinimumBytes", dynamicMemory.Minimum * Constants.Size1M);
                cmd.Parameters.Add("MaximumBytes", dynamicMemory.Maximum * Constants.Size1M);
                cmd.Parameters.Add("Buffer", dynamicMemory.Buffer);
                cmd.Parameters.Add("Priority", dynamicMemory.Priority);
            }
            else
            {
                cmd.Parameters.Add("DynamicMemoryEnabled", false);
            }

            powerShell.Execute(cmd, true, true);
        }
        public static void Update(PowerShellManager powerShell, VirtualMachine vm, int ramMb, DynamicMemory dynamicMemory)
        {
            Command cmd = new Command("Set-VMMemory");

            cmd.Parameters.Add("VMName", vm.Name);
            cmd.Parameters.Add("StartupBytes", ramMb * Constants.Size1M);

            if (dynamicMemory != null && dynamicMemory.Enabled)
            {
                cmd.Parameters.Add("DynamicMemoryEnabled", true);
                cmd.Parameters.Add("MinimumBytes", dynamicMemory.Minimum * Constants.Size1M);
                cmd.Parameters.Add("MaximumBytes", dynamicMemory.Maximum * Constants.Size1M);
                cmd.Parameters.Add("Buffer", dynamicMemory.Buffer);
                cmd.Parameters.Add("Priority", dynamicMemory.Priority);
            }
            else
            {
                cmd.Parameters.Add("DynamicMemoryEnabled", false);
            }

            powerShell.Execute(cmd, true, true);
        }
        public static VirtualMachineNetworkAdapter[] Get(PowerShellManager powerShell, string vmName)
        {
            List<VirtualMachineNetworkAdapter> adapters = new List<VirtualMachineNetworkAdapter>();

            Command cmd = new Command("Get-VMNetworkAdapter");
            if (!string.IsNullOrEmpty(vmName)) cmd.Parameters.Add("VMName", vmName);

            Collection<PSObject> result = powerShell.Execute(cmd, true);
            if (result != null && result.Count > 0)
            {
                foreach (PSObject psAdapter in result)
                {
                    VirtualMachineNetworkAdapter adapter = new VirtualMachineNetworkAdapter();

                    adapter.Name = psAdapter.GetString("Name");
                    adapter.MacAddress = psAdapter.GetString("MacAddress");
                    adapter.SwitchName = psAdapter.GetString("SwitchName");

                    adapters.Add(adapter);
                }
            }
            return adapters.ToArray();
        }
        public static void SetReplicaServer(PowerShellManager powerShell, bool enabled, string remoteServer, string thumbprint, string storagePath)
        {
            Command cmd = new Command("Set-VMReplicationServer");
            cmd.Parameters.Add("ReplicationEnabled", enabled);

            if (!string.IsNullOrEmpty(remoteServer))
            {
                cmd.Parameters.Add("ComputerName", remoteServer);
            }

            if (!string.IsNullOrEmpty(thumbprint))
            {
                cmd.Parameters.Add("AllowedAuthenticationType", "Certificate");
                cmd.Parameters.Add("CertificateThumbprint", thumbprint);
            }

            if (!string.IsNullOrEmpty(storagePath))
            {
                cmd.Parameters.Add("ReplicationAllowedFromAnyServer", true);
                cmd.Parameters.Add("DefaultStorageLocation", storagePath);
            }

            powerShell.Execute(cmd, false);
        }
Exemple #50
0
        public static BiosInfo Get(PowerShellManager powerShell, string name, int generation)
        {
            BiosInfo info = new BiosInfo();

            // for Win2012R2+ and Win8.1+
            if (generation == 2)
            {
                Command cmd = new Command("Get-VMFirmware");

                cmd.Parameters.Add("VMName", name);

                Collection <PSObject> result = powerShell.Execute(cmd, true);
                if (result != null && result.Count > 0)
                {
                    info.NumLockEnabled = true;

                    List <string> startupOrders = new List <string>();
                    info.BootFromCD = false;

                    foreach (dynamic item in (IEnumerable)result[0].GetProperty("BootOrder"))
                    {
                        string bootType = item.BootType.ToString();

                        // bootFromCD
                        if (!startupOrders.Any() && bootType == "Drive")
                        {
                            var device = item.Device;
                            info.BootFromCD = device.GetType().Name == "DvdDrive";
                        }

                        // startupOrders
                        startupOrders.Add(bootType);
                    }

                    info.StartupOrder = startupOrders.ToArray();
                }
            }
            // for others win and linux
            else
            {
                Command cmd = new Command("Get-VMBios");

                cmd.Parameters.Add("VMName", name);

                Collection <PSObject> result = powerShell.Execute(cmd, true);
                if (result != null && result.Count > 0)
                {
                    info.NumLockEnabled = Convert.ToBoolean(result[0].GetProperty("NumLockEnabled"));

                    List <string> startupOrders = new List <string>();

                    foreach (var item in (IEnumerable)result[0].GetProperty("StartupOrder"))
                    {
                        startupOrders.Add(item.ToString());
                    }

                    info.StartupOrder = startupOrders.ToArray();
                    info.BootFromCD   = false;
                    if (info.StartupOrder != null && info.StartupOrder.Length > 0)
                    {
                        info.BootFromCD = info.StartupOrder[0] == "CD";
                    }
                }
            }

            return(info);
        }
 public static VirtualMachineNetworkAdapter Get(PowerShellManager powerShell, string vmName, string macAddress)
 {
     var adapters = Get(powerShell, vmName);
     return adapters.FirstOrDefault(a => a.MacAddress == macAddress);
 }
        public static void Delete(PowerShellManager powerShell, string vmName, string macAddress)
        {
            var networkAdapter = Get(powerShell, vmName, macAddress);

            if (networkAdapter == null)
                return;

            Delete(powerShell, vmName, networkAdapter);
        }
        public static void Delete(PowerShellManager powerShell, string vmName, VirtualMachineNetworkAdapter networkAdapter)
        {
            Command cmd = new Command("Remove-VMNetworkAdapter");

            cmd.Parameters.Add("VMName", vmName);
            cmd.Parameters.Add("Name", networkAdapter.Name);

            powerShell.Execute(cmd, true);
        }
Exemple #54
0
        public static VirtualMachineNetworkAdapter Get(PowerShellManager powerShell, string vmName, string macAddress)
        {
            var adapters = Get(powerShell, vmName);

            return(adapters.FirstOrDefault(a => a.MacAddress == macAddress));
        }
Exemple #55
0
        public static BiosInfo Get(PowerShellManager powerShell, string name, int generation)
        {
            BiosInfo info = new BiosInfo();

            // for Win2012R2+ and Win8.1+
            if (generation == 2)
            {
                Command cmd = new Command("Get-VMFirmware");

                cmd.Parameters.Add("VMName", name);

                Collection<PSObject> result = powerShell.Execute(cmd, true);
                if (result != null && result.Count > 0)
                {
                    info.NumLockEnabled = true;

                    List<string> startupOrders = new List<string>();
                    info.BootFromCD = false;

                    foreach (dynamic item in (IEnumerable)result[0].GetProperty("BootOrder"))
                    {
                        string bootType = item.BootType.ToString();

                        // bootFromCD
                        if (!startupOrders.Any() && bootType == "Drive")
                        {
                            var device = item.Device;
                            info.BootFromCD = device.GetType().Name == "DvdDrive";
                        }

                        // startupOrders
                        startupOrders.Add(bootType);
                    }

                    info.StartupOrder = startupOrders.ToArray();
                }
            }
            // for others win and linux
            else
            {
                Command cmd = new Command("Get-VMBios");

                cmd.Parameters.Add("VMName", name);

                Collection<PSObject> result = powerShell.Execute(cmd, true);
                if (result != null && result.Count > 0)
                {
                    info.NumLockEnabled = Convert.ToBoolean(result[0].GetProperty("NumLockEnabled"));

                    List<string> startupOrders = new List<string>();

                    foreach (var item in (IEnumerable)result[0].GetProperty("StartupOrder"))
                        startupOrders.Add(item.ToString());

                    info.StartupOrder = startupOrders.ToArray();
                    info.BootFromCD = false;
                    if (info.StartupOrder != null && info.StartupOrder.Length > 0)
                        info.BootFromCD = info.StartupOrder[0] == "CD";
                }
            }

            return info;
        }