예제 #1
0
        public VirtualSwitch CreateSwitch(string name)
        {
            // Create private switch

            HostedSolutionLog.LogStart("CreateSwitch");
            HostedSolutionLog.DebugInfo("Name: {0}", name);

            VirtualSwitch virtualSwitch = null;

            try
            {
                Command cmd = new Command("New-VMSwitch");

                cmd.Parameters.Add("SwitchType", "Private");
                cmd.Parameters.Add("Name", name);

                Collection<PSObject> result = PowerShell.Execute(cmd, true);
                if (result != null && result.Count > 0)
                {
                    virtualSwitch = new VirtualSwitch();
                    virtualSwitch.SwitchId = result[0].GetString("Name");
                    virtualSwitch.Name = result[0].GetString("Name");
                    virtualSwitch.SwitchType = result[0].GetString("SwitchType");
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("CreateSwitch", ex);
                throw;
            }

            HostedSolutionLog.LogEnd("CreateSwitch");
            return virtualSwitch;
        }
예제 #2
0
        private List<VirtualSwitch> GetSwitches(string computerName, string type)
        {
            HostedSolutionLog.LogStart("GetSwitches");
            HostedSolutionLog.DebugInfo("ComputerName: {0}", computerName);

            List<VirtualSwitch> switches = new List<VirtualSwitch>();

            try
            {

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

                // Not needed as the PowerShellManager adds the computer name
                if (!string.IsNullOrEmpty(computerName)) cmd.Parameters.Add("ComputerName", computerName);
                if (!string.IsNullOrEmpty(type)) cmd.Parameters.Add("SwitchType", type);

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

                foreach (PSObject current in result)
                {
                    VirtualSwitch sw = new VirtualSwitch();
                    sw.SwitchId = current.GetProperty("Name").ToString();
                    sw.Name = current.GetProperty("Name").ToString();
                    sw.SwitchType = current.GetProperty("SwitchType").ToString();
                    switches.Add(sw);
                }
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("GetSwitches", ex);
                throw;
            }

            HostedSolutionLog.LogEnd("GetSwitches");
            return switches;
        }
예제 #3
0
        private VirtualSwitch CreateSwitchFromWmiObject(ManagementObject objSwitch)
        {
            if (objSwitch == null || objSwitch.Properties.Count == 0)
                return null;

            VirtualSwitch sw = new VirtualSwitch();
            sw.SwitchId = (string)objSwitch["Name"];
            sw.Name = (string)objSwitch["ElementName"];
            return sw;
        }
예제 #4
0
 private void DeleteVirtualSwitchServiceItem(VirtualSwitch vs)
 {
     try
     {
         // delete virtual switch
         DeleteSwitch(vs.SwitchId);
     }
     catch (Exception ex)
     {
         HostedSolutionLog.LogError(String.Format("Error deleting Virtual Switch '{0}'", vs.Name), ex);
     }
 }
예제 #5
0
        public List<VirtualSwitch> GetExternalSwitches(string computerName)
        {
            Wmi cwmi = new Wmi(computerName, WMI_VIRTUALIZATION_NAMESPACE);

            Dictionary<string, string> switches = new Dictionary<string, string>();
            List<VirtualSwitch> list = new List<VirtualSwitch>();

            // load external adapters
            Dictionary<string, string> adapters = new Dictionary<string, string>();
            ManagementObjectCollection objAdapters = cwmi.GetWmiObjects("Msvm_ExternalEthernetPort");
            foreach (ManagementObject objAdapter in objAdapters)
                adapters.Add((string)objAdapter["DeviceID"], "1");

            // get active connections
            ManagementObjectCollection objConnections = cwmi.GetWmiObjects("Msvm_ActiveConnection");
            foreach (ManagementObject objConnection in objConnections)
            {
                // check LAN andpoint
                ManagementObject objLanEndpoint = new ManagementObject(new ManagementPath((string)objConnection["Dependent"]));
                string endpointName = (string)objLanEndpoint["Name"];

                if (!endpointName.StartsWith("/DEVICE/"))
                    continue;

                endpointName = endpointName.Substring(8);

                if (adapters.ContainsKey(endpointName))
                {
                    // get switch port
                    ManagementObject objPort = new ManagementObject(new ManagementPath((string)objConnection["Antecedent"]));
                    string switchId = (string)objPort["SystemName"];
                    if (switches.ContainsKey(switchId))
                        continue;

                    // add info about switch
                    ManagementObject objSwitch = cwmi.GetRelatedWmiObject(objPort, "Msvm_VirtualSwitch");
                    switches.Add(switchId, (string)objSwitch["ElementName"]);
                }
            }

            foreach (string switchId in switches.Keys)
            {
                VirtualSwitch sw = new VirtualSwitch();
                sw.SwitchId = switchId;
                sw.Name = switches[switchId];
                list.Add(sw);
            }

            return list;
        }