VMware {code} Community
cmartinvalle
Contributor
Contributor

Help with a script that tries to gather info

Hi,

I have this piece of code:

my $vm_view = Vim::find_entity_views(view_type => 'VirtualMachine');
my $vnic_name;
my $vnic_device;
foreach(@$vm_view) {
        my $vm_name = $_->summary->config->name;
        my $devices =$_->config->hardware->device;
        my $mac_string;
        my $network = "";
        foreach(@$devices) {
                if($_->isa("VirtualEthernetCard")) {
                        $mac_string .= "\t\t[" . $_->deviceInfo->label . "] : " . $_->macAddress . "\n";
                }
                if ($_->deviceInfo->label eq $vnic_name){
                        $vnic_device=$_;
                        print "\t$vnic_device\n";
                        my $currMac = $vnic_device->macAddress;
                        print "\t$currMac\n";
                        my $network = Vim::get_view(mo_ref => $_->backing->network, properties => ['name']);
                        print "\t" . $network->{'name'} . "\n";
                }
        }

And when executing it, the output is:

Undefined subroutine &VirtualEthernetCardDistributedVirtualPortBackingInfo::network called at getAllVMMacs.pl line 38

But I want to gather info from the object VirtualEthernetCardNetworkBackingInfo, not from VirtualEthernetCardDistributedVirtualPortBackingInfo, I suppose the problem is in this line: "my $network = Vim::get_view(mo_ref => $_->backing->network, properties => ['name']);" but I don't know how to invoke to the right object, does anyone know how to do it?

Many thanks.

0 Kudos
4 Replies
stumpr
Virtuoso
Virtuoso

You have to check your backing type since there are differences between a DistributeVirtualPortgroup and a standard Portgroup.

Reuben Stump | http://www.virtuin.com | @ReubenStump
cmartinvalle
Contributor
Contributor

OK, that's it! Many thanks!

It only works with Standard Switches. Do you know who is the right object and proporti for Distributed Switches? I'm trying with some of them (VirtualEthernetCardDistributedVirtualPortBackingInfo, DistributedVirtualSwitch, DistributedVirtualPortGroup, etc.) but still without success Smiley Sad

0 Kudos
stumpr
Virtuoso
Virtuoso

Working from memory here...

Your backing type in the case of a Distributed Virtual Switch Portgroup will be a 'VirtualEthernetCardDistributedVirtualPortBackingInfo'.  From that, you can get the 'portgroupKey'.  That key should be a UUID type value, which you can use to find the DistributedVirtualPortgroup by comparing it with the DistributedVirtualPortgroup.key values.  That will have a 'name' property that is probably the value you want (portgroup name).

You can probably do something like the following once you get your portgroupKey -

$pgKey = <key value from VirtualEthernetCardDistributedVirtualPortBackingInfo>;

$dvpg = find_entity_view( view_type => 'DistributedVirtualPortgroup', filter => { 'key' => $pgKey }, properties => [ 'name' ] );

print $dvpg->{'name'} . "\n";

If you were looking for speed in your script, you could fetch all the Network and DistributedVirtualPortgroup entities and store them in a hash for later lookup to avoid the multiple SDK calls.

Reuben Stump | http://www.virtuin.com | @ReubenStump
cmartinvalle
Contributor
Contributor

Thank you very much! I will give it a try this week and will share the results with you. From my ignorance, it seems to be right!

0 Kudos