コード例 #1
0
        // TODO: Split into Add and Connect methods
        internal static void AddNetworAdapter(this ManagementScope scope, string vmIdentity, string switchName)
        {
            var virtualMachine           = scope.GetVirtualMachine(vmIdentity);
            var virtualSystemSettingData = virtualMachine.GetVirtualSystemSettingData();

            var defaultRessourcePoolForSyntheticEthernetPorts = scope.GetDefaultRessourcePoolForSyntheticEthernetPorts();
            var allocationSettingData = defaultRessourcePoolForSyntheticEthernetPorts.GetAllocationSettingDataFromDefaultRessourcePool();

            allocationSettingData["VirtualSystemIdentifiers"] = new[] { Guid.NewGuid().ToString("B") };
            allocationSettingData["ElementName"]      = "Network Adapter";
            allocationSettingData["StaticMacAddress"] = false;
            allocationSettingData.Put();

            var outParameters = scope.AddResourceSettings(virtualSystemSettingData, allocationSettingData);

            var resultingRessourceSettings = outParameters["ResultingResourceSettings"] as string[];

            var syntheticEthernetPortSettingsData = new ManagementObject(resultingRessourceSettings[0]);

            // --------------- Connect Adapter and Switch

            var defaultRessourcePoolForEthernetConnection = scope.GetDefaultRessourcePoolForEthernetConnection();
            var ethernetPortAllocationSettingData         = defaultRessourcePoolForEthernetConnection.GetAllocationSettingDataFromDefaultRessourcePool();

            var virtualSwitch = scope.GetVirtualSwitch(switchName);

            ethernetPortAllocationSettingData["Parent"]       = syntheticEthernetPortSettingsData;
            ethernetPortAllocationSettingData["HostResource"] = new[] { virtualSwitch };

            scope.AddResourceSettings(virtualSystemSettingData, ethernetPortAllocationSettingData);
        }