I have trawled the net looking for a way to add a network adapter to a virtual machine and specify the port group/ network name from the command line or with vi perl and have found no examples (Or change the port group of an existing VM network adapter). Adding an adapter with vmreconfig.pl is easy but there's no where to set the port group (which contains a vlan id specifically for the new VM that's just been cloned). I am creating a full automated system and this feature seems to be missing or I've just plan missed it. Can anyone help ?
Here's code thats close to what we use:
#$netname is a string of the network name, host view is the moref of the host you are deploying to
my $net_vm_dev_conf_spec = create_net_device(netname => $networkname, hostview => $host_view);
create virtual device config spec for network card
================================================
sub create_net_device {
my %args = @_;
my $network_name = $args{netname};
my $host_view = $args;
if($network_name) {
my $network_list = Vim::get_views(mo_ref_array => $host_view->network);
foreach (@$network_list) {
if($network_name eq $_->name) {
my $network = $_;
my $nic_backing_info =
VirtualEthernetCardNetworkBackingInfo->new(deviceName => $network_name,
network => $network);
my $vd_connect_info =
VirtualDeviceConnectInfo->new(allowGuestControl => 1,
connected => 1,
startConnected => 1);
my $nic = VirtualE1000->new(backing => $nic_backing_info,
key => 9999,
unitNumber => undef,
addressType => 'generated',
connectable => $vd_connect_info);
my $nic_vm_dev_conf_spec =
VirtualDeviceConfigSpec->new(device => $nic,
operation => VirtualDeviceConfigSpecOperation->new('add'));
return $nic_vm_dev_conf_spec;
}
}
}
}
#then create a config spec
my $config_spec = VirtualMachineConfigSpec->new(
);
#Then apply this config spec to a clone spec or something.
--Matt
VCP, vExpert, Unix Geek
Here's code thats close to what we use:
#$netname is a string of the network name, host view is the moref of the host you are deploying to
my $net_vm_dev_conf_spec = create_net_device(netname => $networkname, hostview => $host_view);
create virtual device config spec for network card
================================================
sub create_net_device {
my %args = @_;
my $network_name = $args{netname};
my $host_view = $args;
if($network_name) {
my $network_list = Vim::get_views(mo_ref_array => $host_view->network);
foreach (@$network_list) {
if($network_name eq $_->name) {
my $network = $_;
my $nic_backing_info =
VirtualEthernetCardNetworkBackingInfo->new(deviceName => $network_name,
network => $network);
my $vd_connect_info =
VirtualDeviceConnectInfo->new(allowGuestControl => 1,
connected => 1,
startConnected => 1);
my $nic = VirtualE1000->new(backing => $nic_backing_info,
key => 9999,
unitNumber => undef,
addressType => 'generated',
connectable => $vd_connect_info);
my $nic_vm_dev_conf_spec =
VirtualDeviceConfigSpec->new(device => $nic,
operation => VirtualDeviceConfigSpecOperation->new('add'));
return $nic_vm_dev_conf_spec;
}
}
}
}
#then create a config spec
my $config_spec = VirtualMachineConfigSpec->new(
);
#Then apply this config spec to a clone spec or something.
--Matt
VCP, vExpert, Unix Geek