通过WMI类将VLAN分配给网络适配器

时间:2019-04-13 18:37:22

标签: c# wmi hyper-v

问题:无法使用Msvm_VirtualEthernetSwitchManagementService和AddFeatureSettings方法将VLAN分配给Hyper-V虚拟机。

有人可以指出我在做什么错吗?

我还注意到,如果我使用WMI类创建vNIC,则不会获得Msvm_EthernetPortAllocationSettingData的实例,但是如果手动分配vNIC,则会被创建。.我也难以通过WMI创建Msvm_EthernetPortAllocationSettingData。

从下面的代码中,我得到4096的ReturnValue,这意味着该方法已执行..但未分配任何VLAN。

            ManagementPath syntheticAdapterSettingDataC = new ManagementPath("Msvm_EthernetSwitchPortVlanSettingData");
            String syntheticVlanAdapterId = String.Format("{0}\\C\\952C5004-4465-451C-8CB8-FA9AB382B773\\{1}", adapter.GetPropertyValue("InstanceID"), Guid.NewGuid());

            ManagementClass syntheticAdapterClassC =
               new ManagementClass(scope, syntheticAdapterSettingDataC, objectOptions)
               {
                   ["AccessVlanId"] = 55,
                   ["Caption"] = "Ethernet Switch Port VLAN Settings",
                   ["Description"] = "Represents the vlan setting data.",
                   ["ElementName"] = "Ethernet Switch Port VLAN Settings",
                   ["InstanceID"] = syntheticVlanAdapterId,
                   ["NativeVlanId"] = 0,
                   ["OperationMode"] = 1,
                   ["PrimaryVlanId"] = 0,
                   ["PruneVlanIdArray"] = null,
                   ["PvlanMode"] = 0,
                   ["SecondaryVlanId"] = 0,
                   ["SecondaryVlanIdArray"] = null,
                   ["TrunkVlanIdArray"] = null,
               };
            var syntheticAdapterC = syntheticAdapterClassC.CreateInstance();

            ManagementPath VirtualEthernetSwitchManagementServicePath= new ManagementPath("Msvm_VirtualEthernetSwitchManagementService");
            ManagementClass VirtualEthernetSwitchManagementServiceClass = new ManagementClass(scope, VirtualEthernetSwitchManagementServicePath, objectOptions);

            ManagementBaseObject inParams = VirtualEthernetSwitchManagementServiceClass.GetMethodParameters("AddFeatureSettings");


            string queryFeature = string.Format("select * from Msvm_FeatureSettingData Where InstanceID = 'Microsoft:Definition\\\\952C5004-4465-451C-8CB8-FA9AB382B773\\\\Default'");

            ManagementObjectSearcher searcherFeature = new ManagementObjectSearcher(scope, new ObjectQuery(queryFeature));

            ManagementObjectCollection features = searcherFeature.Get();

            ManagementObject feature = null;

            foreach (ManagementObject instance in features)
            {
                feature = instance;
                break;
            }

            string[] syntheticAdapterSettingsC = new string[1];
            syntheticAdapterSettingsC[0] = syntheticAdapterC.GetText(TextFormat.CimDtd20);



            inParams["AffectedConfiguration"] = feature.GetText(TextFormat.CimDtd20);

            inParams["FeatureSettings"] = syntheticAdapterSettingsC;

            ManagementObject service = null;

            foreach (ManagementObject instance in VirtualEthernetSwitchManagementServiceClass.GetInstances())
            {
                service = instance;
            }

            ManagementBaseObject vlanOut = service.InvokeMethod("AddFeatureSettings", inParams, null);

2 个答案:

答案 0 :(得分:0)

经过实验,我找到了答案。您需要执行的操作是使用“ Msvm_VirtualSystemManagementService ”类使用“ AddResourceSettings”方法创建( Msvm_EthernetPortAllocationSettingData )实例(如果已经拥有,则指向一个实例)。

要使用“ AddResourceSettings”方法,您需要定义:

  • AffectedConfiguration属性,它是 Msvm_VirtualSystemSettingData 类的一个实例
  • ResourceSettings属性,它是 Msvm_EthernetPortAllocationSettingData 的实例,但是您需要将此实例放入数组中。

现在您可以分配VLAN了。您将需要使用 Msvm_VirtualSystemManagementService 类和“ AddFeatureSettings”方法来创建 Msvm_EthernetSwitchPortVlanSettingData 的实例。

要使用“ AddFeatureSettings”方法,您需要定义:

  • AffectedConfiguration,它是 Msvm_EthernetPortAllocationSettingData
  • 的实例
  • FeatureSettings,它是 Msvm_EthernetSwitchPortVlanSettingData 的实例,也是数组

就是这样。

干杯!

答案 1 :(得分:0)

ya ..知道它有点旧,但是让其他尝试实现此目标的人省去了头痛。以下代码将网络适配器分配给VM并设置VLAN。请记住,我的“ _dataFields”是具有虚拟机数据的结构,因此您在这里只需更改几下即可。

添加新的网络适配器并设置VLAN

    /// <summary>
    /// For the given virtual machine, this sample adds a new Network Adapter device and 
    /// connects it to the specified switch. Note that in order to add a new Network Adapter 
    /// device to the virtual machine, the virtual machine must be in the power off state.
    /// Also note that the maximum number of Network Adapter devices that may be configured
    /// on a virtual machine is 8.
    /// </summary>
    public void ConnectVmToSwitch()
    {
        using (ManagementObject managementService = WmiUtils.GetVirtualMachineManagementService(_dataFields._scope))

        //
        // Find the Ethernet switch we want to connect to.
        //
        using (ManagementObject ethernetSwitch = NetworkUtils.FindEthernetSwitch(_dataFields.SwitchName, _dataFields._scope))

        //
        // Find the virtual machine we want to connect.
        //
        using (ManagementObject virtualMachine = WmiUtils.GetVirtualMachine(_dataFields.VmName, _dataFields._scope))

        //
        // Get the virtual machine's settings object which is used to make configuration changes.
        //
        using (ManagementObject virtualMachineSettings = WmiUtils.GetVirtualMachineSettings(virtualMachine))

        //
        // Add a new synthetic Network Adapter device to the virtual machine.
        //
        using (ManagementObject syntheticAdapter = NetworkUtils.AddSyntheticAdapter(virtualMachine, _dataFields._scope))

        //
        // Now that we have added a network adapter to the virtual machine we can configure its
        // connection settings.
        //
        using (ManagementObject connectionSettingsToAdd = NetworkUtils.GetDefaultEthernetPortAllocationSettingData(_dataFields._scope))
        {
            connectionSettingsToAdd["Parent"] = syntheticAdapter.Path.Path;
            connectionSettingsToAdd["HostResource"] = new string[] { ethernetSwitch.Path.Path };

            //
            // Now add the connection settings.
            //
            using (ManagementBaseObject addConnectionInParams = managementService.GetMethodParameters("AddResourceSettings"))
            {
                addConnectionInParams["AffectedConfiguration"] = virtualMachineSettings.Path.Path;
                addConnectionInParams["ResourceSettings"] = new string[] { connectionSettingsToAdd.GetText(TextFormat.WmiDtd20) };

                using (ManagementBaseObject addConnectionOutParams = managementService.InvokeMethod("AddResourceSettings", addConnectionInParams, null))
                {
                    WmiUtils.ValidateOutput(addConnectionOutParams, _dataFields._scope);

                    if (_dataFields.VlanID > 0)
                    {
                        string[] syntheticAdapterResult = (string[])addConnectionOutParams["ResultingResourceSettings"]; // Msvm_EthernetPortAllocationSettingData return object
                        string syntheticAdapterPath = syntheticAdapterResult[0]; // Msvm_EthernetPortAllocationSettingData path

                        using (ManagementClass vlanSettingsData = new ManagementClass("Msvm_EthernetSwitchPortVlanSettingData"))
                        {
                            vlanSettingsData.Scope = _dataFields._scope;
                            using (ManagementObject vlanData = vlanSettingsData.CreateInstance())
                            {
                                vlanData["AccessVlanId"] = _dataFields.VlanID;
                                vlanData["OperationMode"] = (uint)_dataFields.VLANOperationalModes;

                                // Modify the VM settings.
                                using (ManagementBaseObject inParams = managementService.GetMethodParameters("AddFeatureSettings"))
                                {
                                    inParams["AffectedConfiguration"] = syntheticAdapterPath;
                                    inParams["FeatureSettings"] = new string[] { vlanData.GetText(TextFormat.CimDtd20) };

                                    using (ManagementBaseObject outParams = managementService.InvokeMethod("AddFeatureSettings", inParams, null))
                                    {
                                        WmiUtils.ValidateOutput(outParams, _dataFields._scope);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

将交换机分配给VM的原始代码来自https://github.com/microsoft/Windows-classic-samples/blob/1d363ff4bd17d8e20415b92e2ee989d615cc0d91/Samples/Hyper-V/Networking/cs/ConnectVmToSwitch.cs,我刚刚对其进行了扩展以支持VLAN的使用。我没有任何参数在通话中,因为所有数据都在结构中。

在VM上的现有网络适配器上添加/修改VLAN

    /// <summary>
    /// Gets any virtual machine's management object
    /// </summary>
    /// <param name="managementObject">Any management object</param>
    /// <returns>Any virtual machine's management object.</returns>
    public static ManagementObject
    GetVirtualMachineManagementObject(ManagementObject managementObject, string className)
    {
        using (ManagementObjectCollection settingsCollection = managementObject.GetRelated(className))
        {
            ManagementObject virtualMachineSettings = GetFirstObjectFromCollection(settingsCollection);
            return virtualMachineSettings;
        }
    }

    /// <summary>
    /// For the given virtual machine, this sample will add / modfiy VLAN on existing network adapter
    /// </summary>
    public void SetVLANToVMNetworkAdapter()
    {
        ManagementObject syntheticAdapter = null;
        bool vlanAlreadySet = false;

        using (ManagementObject managementService = WmiUtils.GetVirtualMachineManagementService(_dataFields._scope))

        //
        // Find the virtual machine we want to connect.
        //
        using (ManagementObject virtualMachine = WmiUtils.GetVirtualMachine(_dataFields.VmName, _dataFields._scope))

        //
        // Now that we have added a network adapter to the virtual machine we can configure its
        // connection settings.
        //
        using (ManagementObjectCollection findConnections = NetworkUtils.FindConnections(virtualMachine, _dataFields._scope))
        {
            if (findConnections.Count > 0)
            {
                foreach (ManagementObject connection in findConnections)
                {
                    using (ManagementObjectCollection vmSwitches = connection.GetRelated("Msvm_SyntheticEthernetPortSettingData"))
                    {
                        if (vmSwitches.Count > 0)
                        {
                            foreach (ManagementObject vmSwitch in vmSwitches)
                            {
                                if (vmSwitch["ElementName"].ToString() == _dataFields.NetworkAdapterName)
                                {
                                    //
                                    // Got adapter on VM, lock it to connection object since we need connection path
                                    // for vlan modifications
                                    //
                                    syntheticAdapter = connection;

                                    //
                                    // Got VLAN defiinition based on connection lock for vlan modifications
                                    //
                                    using (ManagementObjectCollection vmSwitcheVLANs = syntheticAdapter.GetRelated("Msvm_EthernetSwitchPortVlanSettingData"))
                                    {
                                        if (vmSwitcheVLANs.Count > 0)
                                            vlanAlreadySet = true;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (syntheticAdapter != null && _dataFields.VlanID > 0)
            {
                string syntheticAdapterPath = syntheticAdapter.Path.Path;
                if (vlanAlreadySet)
                {
                    // VLAN is already set on adapter, change operation
                    // Modify the VM settings.
                    using (ManagementObject vlanData = WmiUtils.GetVirtualMachineManagementObject(syntheticAdapter, "Msvm_EthernetSwitchPortVlanSettingData"))
                    {
                        vlanData["AccessVlanId"] = _dataFields.VlanID;
                        vlanData["OperationMode"] = (uint)_dataFields.VLANOperationalModes;
                        using (ManagementBaseObject inParams = managementService.GetMethodParameters("ModifyFeatureSettings"))
                        {
                            inParams["FeatureSettings"] = new string[] { vlanData.GetText(TextFormat.CimDtd20) };
                            using (ManagementBaseObject outParams = managementService.InvokeMethod("ModifyFeatureSettings", inParams, null))
                            {
                                WmiUtils.ValidateOutput(outParams, _dataFields._scope);
                            }
                        }
                    }
                }
                else
                {
                    using (ManagementClass vlanSettingsData = new ManagementClass("Msvm_EthernetSwitchPortVlanSettingData"))
                    {
                        vlanSettingsData.Scope = _dataFields._scope;
                        using (ManagementObject vlanData = vlanSettingsData.CreateInstance())
                        {
                            vlanData["AccessVlanId"] = _dataFields.VlanID;
                            vlanData["OperationMode"] = (uint)_dataFields.VLANOperationalModes;

                            using (ManagementBaseObject inParams = managementService.GetMethodParameters("AddFeatureSettings"))
                            {
                                inParams["AffectedConfiguration"] = syntheticAdapterPath;
                                inParams["FeatureSettings"] = new string[] { vlanData.GetText(TextFormat.CimDtd20) };

                                using (ManagementBaseObject outParams = managementService.InvokeMethod("AddFeatureSettings", inParams, null))
                                {
                                    WmiUtils.ValidateOutput(outParams, _dataFields._scope);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

从VM上的网络适配器删除VLAN

    /// <summary>
    /// For the given virtual machine, this sample will delete VLAN on existing network adapter 
    /// </summary>
    public void RemoveVLANFromVMNetworkAdapter()
    {
        using (ManagementObject managementService = WmiUtils.GetVirtualMachineManagementService(_dataFields._scope))

        //
        // Find the virtual machine we want to connect.
        //
        using (ManagementObject virtualMachine = WmiUtils.GetVirtualMachine(_dataFields.VmName, _dataFields._scope))

        //
        // Now that we have added a network adapter to the virtual machine we can configure its
        // connection settings.
        //
        using (ManagementObjectCollection findConnections = NetworkUtils.FindConnections(virtualMachine, _dataFields._scope))
        {
            if (findConnections.Count > 0)
            {
                foreach (ManagementObject connection in findConnections)
                {
                    // Get network adapter on virtual machine
                    using (ManagementObjectCollection vmSwitches = connection.GetRelated("Msvm_SyntheticEthernetPortSettingData"))
                    {
                        if (vmSwitches.Count > 0)
                        {
                            foreach (ManagementObject vmSwitch in vmSwitches)
                            {
                                if (vmSwitch["ElementName"].ToString() == _dataFields.NetworkAdapterName)
                                {
                                    // Get vlan settings data from network adapter
                                    using (ManagementObjectCollection vmSwitcheVLANs = connection.GetRelated("Msvm_EthernetSwitchPortVlanSettingData"))
                                    {
                                        if (vmSwitcheVLANs.Count > 0)
                                        {
                                            // Get first objecz
                                            using (ManagementObject vlanData = WmiUtils.GetFirstObjectFromCollection(vmSwitcheVLANs))
                                            {
                                                using (ManagementBaseObject inParams = managementService.GetMethodParameters("RemoveFeatureSettings"))
                                                {
                                                    // Remove it
                                                    inParams["FeatureSettings"] = new string[] { vlanData.Path.Path };
                                                    using (ManagementBaseObject outParams = managementService.InvokeMethod("RemoveFeatureSettings", inParams, null))
                                                    {
                                                        WmiUtils.ValidateOutput(outParams, _dataFields._scope);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

请记住,我不使用通用的网络适配器名称,而是在创建VM时使用个性化的适配器名称。这样,我可以在需要时轻松获取适配器数据。

想要为适配器添加个性化名称的用户可以编辑AddSyntheticAdapter(ManagementObject virtualMachine,ManagementScope范围)函数,并在adapterToAdd [“ ElementName”]对象中包含其他名称。

希望这可以帮助勇敢地通过WMI发展的灵魂! :)