Hiya,
I am trying to change the Virtual Switch a VM is connected to, using the VI SDK and C#. As a test, I am able to change the macAddress of a given VirtualEthernetCard, but if I try to change the card's 'deviceInfo.summary' (which i assume is the correct property to change to change the connected vSwitch) nothing happens.
Here is some sample code:
private static void changeNetwork(VimService service,
ManagedObjectReference vm,
VirtualEthernetCard card,
string newNetworkName)
{
card.macAddress = "10:10:20:30:40:50";//This works...
card.deviceInfo.summary = newNetworkName;//But this doesn't
VirtualDeviceConfigSpec spec = new VirtualDeviceConfigSpec();
spec.device = card;
spec.operation = VirtualDeviceConfigSpecOperation.edit;
spec.operationSpecified = true;
VirtualMachineConfigSpec config = new VirtualMachineConfigSpec();
config.deviceChange = new VirtualDeviceConfigSpec[] { spec };
service.ReconfigVM_Task(vm, config);
}
Hope you can help. Either I'm close, or I'm beating around completely the wrong bush!
Try the following:
VirtualEthernetCardNetworkBackingInfo bi= new VirtualEthernetCardNetworkBackingInfo();
bi.network = <ManagedObjectReference to your new Network> // NOT the name, but ManagedObjectReference
card.backing = bi
Steve JIN, VMware Engineering
I managed to get back to this, and got it working. For completeness, the working code is:
private static VirtualDeviceConfigSpec getDeviceConfigSpec(VirtualEthernetCard card,
ManagedObjectReference newNetwork, string newNetworkName)
{
VirtualEthernetCardNetworkBackingInfo newBI = new VirtualEthernetCardNetworkBackingInfo();
newBI.network = Ref;
newBI.deviceName = newNetworkName;
card.backing = newBI;
VirtualDeviceConfigSpec spec = new VirtualDeviceConfigSpec();
spec.device = card;
spec.operation = VirtualDeviceConfigSpecOperation.edit;
spec.operationSpecified = true;
return spec;
}
Thanks Steve for your answer.
Hello, is it possible to do exactly the same code but with VI Perl (not easy to understand the ReconfigVM function ... )
to change network label with vm poweredOn
Many Thanks
Hi,
In perl, you can reconfigure the VM in powered on state to change the network as follows:
my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter =>{'name' => $vm_name }); # $network holds the reference to the new network and $networkName has the new network name my $backing_info = VirtualEthernetCardNetworkBackingInfo->new(network => $network, deviceName => $networkName); my $card = VirtualPCNet32->new(backing => $backing_info, key => $key); # here $key is the "key" from the existing PCNet32 Ethernet card attached to a virtual machine. my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit'); my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation, device => $card); my $device_config_specs = [$devspec]; my $vmspec = VirtualMachineConfigSpec->new(deviceChange => $device_config_specs); eval { $vm_view->ReconfigVM( spec => $vmspec ); Util::trace(0,"\nVirtual machine '" . $vm_view->name . "' is reconfigured successfully.\n"); };
Hope this is helpful.
Neha
Ok perfect, thank you Neha
Regis
Thanks for the perl code snippet. It was very useful. If anyone's interested, I found a way to not have to recreate the network device completely (which saves you from having to determine if the network device was PCNet32, E1000, or whatever).
my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter =>{'name' => $vm_name }); # $network holds the reference to the new network and $networkName has the new network name my $backing_info = VirtualEthernetCardNetworkBackingInfo->new(network => $network, deviceName => $networkName); # You must set $netCard to the virtual network device you want to modify $netCard->backing($backing_info); my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit'); my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation, device => $netCard); my $device_config_specs = [$devspec]; my $vmspec = VirtualMachineConfigSpec->new(deviceChange => $device_config_specs); eval { $vm_view->ReconfigVM( spec => $vmspec ); Util::trace(0,"\nVirtual machine '" . $vm_view->name . "' is reconfigured successfully.\n"); };