none
Hyper-V中网卡问题 RRS feed

  • 问题

  •  

    我用WMI创建好VM后,通过 AddVirtualSystemResources 添加了一个默认配置的网卡,不过新添加的网卡是Not Connected,这样的网络在VM里面显示网络无连接,我想知道如何通过WMI改变这个网卡的属性,让它变成已连接的

     

    附上源代码:

     

    Code Snippet

    public void addNetworkAdapter(string vmName,string nicName,string macAddress)

    {

    ManagementObject vm = getVM(vmName);

    ManagementObject virtualSystemService = getVirtualizationObject("Msvm_VirtualSystemManagementService");

    ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("AddVirtualSystemResources");

    ManagementObject SyntheticNicDefault = getDefaultEthernetAdapterSettingData();

    if (macAddress != null)

    {

    SyntheticNicDefault["StaticMacAddress"] = true;

    SyntheticNicDefault["Address"] = macAddress;

    }

    else

    {

    SyntheticNicDefault["StaticMacAddress"] = false;

    }

    SyntheticNicDefault["ElementName"] = nicName;

    String[] identifiers = new String[1];

    identifiers[0] = string.Format("{{{0}}}", Guid.NewGuid());

    SyntheticNicDefault["VirtualSystemIdentifiers"] = identifiers;

    string[] RASDs = new string[1];

    RASDs[0] = SyntheticNicDefault.GetText(TextFormat.CimDtd20);

    inParams["ResourcesettingData"] = RASDs;

    inParams["TargetSystem"] = vm.Path.Path;

    ManagementBaseObject outParams = virtualSystemService.InvokeMethod("AddVirtualSystemResources", inParams, null);

    if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)

    {

    // waiting 10 minute.

    WaitJobCompleted((string)outParams["Job"], 1000 * 60 * 10);

    }

    else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)

    {

    Console.WriteLine("Resources were added successfully.");

    }

    else

    {

    Console.WriteLine("Add virtual system synthetic Ethernet failed with error:{0}", outParams["ReturnValue"]);

    }

    inParams.Dispose();

    outParams.Dispose();

    vm.Dispose();

    SyntheticNicDefault.Dispose();

    virtualSystemService.Dispose();

    }

    private ManagementObject getDefaultEthernetAdapterSettingData()

    {

    ManagementObject SyntheticNicDefault = null;

    ManagementScope scope = new ManagementScope(@"\\" + _sServer + @"\" + _hyperVWMINameSpace);

    string query = String.Format("select * from Msvm_ResourcePool where ResourceType = '10' and ResourceSubType ='Microsoft Synthetic Ethernet Port'");

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));

    ManagementObjectCollection poolResources = searcher.Get();

    //Get pool resource allocation ability

    if (poolResources.Count == 1)

    {

    foreach (ManagementObject poolResource in poolResources)

    {

    ManagementObjectCollection allocationCapabilities = poolResource.GetRelated("Msvm_AllocationCapabilities");

    foreach (ManagementObject allocationCapability in allocationCapabilities)

    {

    ManagementObjectCollection settingDatas = allocationCapability.GetRelationships("Msvm_SettingsDefineCapabilities");

    foreach (ManagementObject settingData in settingDatas)

    {

    if (Convert.ToInt16(settingData["ValueRole"]) == (UInt16)0)

    {

    SyntheticNicDefault = new ManagementObject(settingData["PartComponent"].ToString());

    break;

    }

    }

    }

    }

    }

    return SyntheticNicDefault;

    }

     

     

    2008年11月21日 2:02

答案