Ejemplo n.º 1
0
 public static void Delete(PowerShellManager powerShell, VirtualHardDiskInfo[] disks, string serverNameSettings)
 {
     if (disks != null && disks.GetLength(0) > 0)
     {
         foreach (VirtualHardDiskInfo diskItem in disks)
         {
             VirtualHardDiskInfo disk = diskItem;
             do
             {
                 if (!string.IsNullOrEmpty(serverNameSettings))
                 {
                     string cmd = "Invoke-Command -ComputerName " + serverNameSettings + " -ScriptBlock { Remove-item -path " + disk.Path + " }";
                     powerShell.Execute(new Command(cmd, true), false);
                 }
                 else
                 {
                     Command cmd = new Command("Remove-item");
                     cmd.Parameters.Add("path", disk.Path);
                     powerShell.Execute(cmd, false);
                 }
                 // remove all parent disks
                 disk.Path = disk.ParentPath;
                 if (!String.IsNullOrEmpty(disk.Path))
                 {
                     GetVirtualHardDiskDetail(powerShell, disk.Path, ref disk);
                 }
             } while (!String.IsNullOrEmpty(disk.Path));
         }
     }
 }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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);
            try
            {
                powerShell.Execute(cmd, false, true);
            }
            catch
            {
                cmd = new Command("Stop-VM");
                cmd.Parameters.Add("Name", vmName);
                cmd.Parameters.Add("TurnOff");
                powerShell.Execute(cmd, false);
            }
        }
Ejemplo n.º 8
0
        //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);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        public static void Delete(PowerShellManager powerShell, string vmName)
        {
            Command cmd = new Command("Remove-VMSnapshot");

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

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

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

            powerShell.Execute(cmd, true);
        }
Ejemplo n.º 12
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);
            }

            Command cmdvlan = new Command("Get-VMNetworkAdapterVlan");

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

            Collection <PSObject> result     = powerShell.Execute(cmd, true);
            Collection <PSObject> resultvlan = powerShell.Execute(cmdvlan, true);
            int i = 0;

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

                    adapter.Name = psAdapter.GetString("Name");
                    try
                    {
                        adapter.IPAddresses = psAdapter.GetProperty <string[]>("IPAddresses");
                    }
                    catch (Exception ex) { HostedSolution.HostedSolutionLog.LogError("VirtualMachineNetworkAdapter", ex); }
                    adapter.MacAddress = psAdapter.GetString("MacAddress");
                    adapter.SwitchName = psAdapter.GetString("SwitchName");

                    try
                    {
                        adapter.vlan = resultvlan[i].GetInt("AccessVlanId");
                    }
                    catch
                    {
                        adapter.vlan = defaultvlan;
                    }
                    if (adapter.vlan == 0)
                    {
                        adapter.vlan = defaultvlan;
                    }
                    //We can't do that things! https://docs.microsoft.com/en-us/powershell/module/hyper-v/remove-vmnetworkadapter
                    //adapter.Name = String.Format("{0} VLAN: {1}", psAdapter.GetString("Name"), adapter.vlan.ToString());
                    i++;
                    adapters.Add(adapter);
                }
            }
            return(adapters.ToArray());
        }
Ejemplo n.º 13
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, true);
        }
Ejemplo n.º 14
0
        private static VirtualHardDiskInfo GetParentVHD(VirtualHardDiskInfo disk, PowerShellManager powerShell)
        {
            VirtualHardDiskInfo resDisk = disk.Clone();

            while (!String.IsNullOrEmpty(resDisk.ParentPath))
            {
                resDisk.Path = resDisk.ParentPath;
                GetVirtualHardDiskDetail(powerShell, resDisk.Path, ref resDisk);
            }
            return(resDisk);
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
     }
 }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
 public static void Delete(PowerShellManager powerShell, VirtualHardDiskInfo[] disks)
 {
     if (disks != null && disks.GetLength(0) > 0)
     {
         foreach (VirtualHardDiskInfo disk in disks)
         {
             Command cmd = new Command("Remove-item");
             cmd.Parameters.Add("path", disk.Path);
             powerShell.Execute(cmd, true);
         }
     }
 }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
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);
        }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
0
 public VmConfigurationVersionHelper(PowerShellManager powerShell)
 {
     _powerShell = powerShell;
     try
     {
         _build = ConvertNullableToInt32(Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuild", ""));
     }
     catch (Exception ex)
     {
         /* if error then Windows is too old, or MS made BC changes, or Adminstrator broke Windows Registry*/
         HostedSolutionLog.LogWarning("VmConfigurationVersionHelper", ex); //log error, but continue, cause it's not critical
     }
 }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
0
        public static void SetVLAN(PowerShellManager powerShell, string vmName, string adapterName, int vlan)
        {
            if (vlan >= 1)
            {
                Command cmd = new Command("Set-VMNetworkAdapterVlan");

                cmd.Parameters.Add("VMName", vmName);
                cmd.Parameters.Add("VMNetworkAdapterName", adapterName);
                cmd.Parameters.Add("Access");
                cmd.Parameters.Add("VlanId", vlan.ToString());

                powerShell.Execute(cmd, true, true);
            }
        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
0
 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"));
         }
     }
 }
Ejemplo n.º 29
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, true);
        }
Ejemplo n.º 30
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);
                try
                {
                    SetVLAN(powerShell, vm.Name, Constants.EXTERNAL_NETWORK_ADAPTER_NAME, vm.defaultaccessvlan);
                }
                catch (Exception ex)
                {
                    HostedSolution.HostedSolutionLog.LogError("NetworkAdapterHelperSetVLAN", ex);
                }
            }

            // 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);
                try
                {
                    SetVLAN(powerShell, vm.Name, Constants.PRIVATE_NETWORK_ADAPTER_NAME, vm.PrivateNetworkVlan);
                }
                catch (Exception ex)
                {
                    HostedSolution.HostedSolutionLog.LogError("NetworkAdapterHelperSetVLAN", ex);
                }
            }
        }