问题:无法使用 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);
最佳答案
经过试验,我找到了答案。您需要做的是使用“AddResourceSettings”方法使用 Msvm_VirtualSystemManagementService 类创建(或指向一个,如果您已经有的话)Msvm_EthernetPortAllocationSettingData 实例。
要使用“AddResourceSettings”方法,您需要定义:
- AffectedConfiguration 属性,它是 Msvm_VirtualSystemSettingData 类的一个实例
- ResourceSettings 属性,它是Msvm_EthernetPortAllocationSettingData 的实例,但您需要将此实例放在数组中。
现在您已准备好分配 vlan。您需要使用 Msvm_VirtualSystemManagementService 类和“AddFeatureSettings”方法创建 Msvm_EthernetSwitchPortVlanSettingData 的实例。
要使用“AddFeatureSettings”方法,您需要定义:
- AffectedConfiguration,它是 Msvm_EthernetPortAllocationSettingData 的一个实例
- FeatureSettings,它是Msvm_EthernetSwitchPortVlanSettingData的实例,也是数组
就是这样..
干杯!
关于c# - 通过 wmi 类将 vlan 分配给网络适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55668725/