VMware {code} Community
lamw
Community Manager
Community Manager
Jump to solution

get_view MOR Property Filter ?

I was told there might be an option when getting a view of a MOR that allows you to filter out the specific attributes without having to get the entire MOR, I believe it's called a property filter?

I'm trying to just extract the hostname that a VM is being managed by but I don't want the entire view of the hostSystem MOR

I based the following snippet of code from someone stating this is possible:

my $host_view =  Vim::get_view (mo_ref => $vm->runtime->host, properties => );
print $host_view->name,"\n";

I was not able to get this to work and was not able to find anything in the API referencing this feature.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
1 Solution

Accepted Solutions
kri-2
Hot Shot
Hot Shot
Jump to solution

hi,

this works for me, and is much faster:

my $host_view = Vim::get_view(mo_ref=>$vm_view->summary->runtime->host, properties => \['name'\]);

if($host_view){

print $host_view->name;

}

View solution in original post

0 Kudos
9 Replies
kri-2
Hot Shot
Hot Shot
Jump to solution

hi,

this works for me, and is much faster:

my $host_view = Vim::get_view(mo_ref=>$vm_view->summary->runtime->host, properties => \['name'\]);

if($host_view){

print $host_view->name;

}

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

I was trying the property filter on the VM View to just retrieve the datastore name but ran into an error:

#retrieve datastore summary from VM

my $vm_datastore_view = $vm->datastore;

foreach (@$vm_datastore_view) {

my $ds = Vim::get_view(mo_ref => $_, properties => \['summary.name'\]);

$vms_storage{$vm->config->name} = $ds->name;

}

I also tried

my $ds = Vim::get_view(mo_ref => $_->summary, properties => \['name'\] );

$vms_storage{$vm->config->name} = $ds->name;

Am I missing something?

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Yeah, I mentioned this briefly in another post from this weekend I responded to.

I can't see all of your post, the forum mangled your property filter. But if you ask for a property of a property: 'runtime.inMaintenanceMode' for example, the Perl toolkit will sort of condense it to just 'inMaintenanceMode'. So it'll be accessed not as $vm->runtime->inMaintenanceMode but as $vm->inMaintenanceMode. I think they did this for brevity, if you look at VICommon.pm (I think it was here) they just keep popping the path of a property until they get to the last property in the path.

As for your second example, I can't tell if you have your get_view properties array in a good format, the forum mangled it. Insert before any ''\[" or "\]" characters a backslash as you post it.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

I always thought we had to use the tags. Anywho, I just fixed the output, you should be able to see what I tried.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

The first one, with the corrections you applied, should work. What is the error you're getting?

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Can't locate object method "name" via package "Datastore" at ./vmwareHealthCheck.pl line 819.

End Disconnect

Though, I'm able to do the following:

my $ds = Vim::get_view(mo_ref => $_);
$vms_storage{$vm->config->name} = $ds->summary->name;

So, hoping I can make it more efficient with just asking for the datastore name only.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

I made a mistake and got you on the wrong track, my apologies. I should have been more thorough when I went through the VICommon.pm code.

So it's not a pop for the properties as I read incorrectly, that only happens when you don't define the properties. It's simply the path you provide in the properties array values:

my $ds = Vim::get_view(mo_ref => $_, properties => \['summary.name'\] );<Br>

$vms_storage{$vm->config->name} = $ds->{'summary.name'};

This obviously makes more sense otherwise the code would overwrite the managed object name property or face other potential clashes in property names. The code definitely sets the all property to 0 and updates the pathSet, so it should do a minimal RetrieveProperties call. Not setting the properties value in a get_view call will retrieve the entire object's property values.

I actually tested it and it works as advertised this time. Smiley Happy

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Yep, that did the trick! Thanks

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
wila
Immortal
Immortal
Jump to solution

Hi William,

I always thought we had to use the tags. Anywho, I just fixed the output, you should be able to see what I tried.

Use the tags for that. They usually work best for code and well.. the majority of things that you don't want to be reformatted.

Have a peeksy at my document here that discusses on how to do these type of things in the forum:



--

Wil

_____________________________________________________

Visit the new VMware developers wiki at http://www.vi-toolkit.com

| Author of Vimalin. The virtual machine Backup app for VMware Fusion, VMware Workstation and Player |
| More info at vimalin.com | Twitter @wilva
0 Kudos