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());
        }
Exemple #2
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);
        }
Exemple #3
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());
        }