stumpr's Accepted Solutions

So I cleaned up your script a bit, mostly just optimizations. I used the built in Opts:: module to add a hostname parameter to the script instead of using ARGV. I changed it so it checks for a ... See more...
So I cleaned up your script a bit, mostly just optimizations. I used the built in Opts:: module to add a hostname parameter to the script instead of using ARGV. I changed it so it checks for a valid host view before you start your loop, no point in looping through data centers if your hostname isn't an actual valid hostname in your VI inventory. Take a look at the LOOP label, I cleaned it up. You want to exit out of the loop after you find the datacenter. Just to show you how much faster my original script logic is, I also wrote another version of my script using the Perl toolkit. It was >3.5s faster on my standalone ESX host. It's largely because the find_entity_view() functions essentially fetch the entire ManagedObject reference property set, which is fairly intensive. This gets pretty slow in a larger environment and is compounded in a nested loop. I usually try to avoid using find_entity_view() heavily in my scripts for this reason. Feel free to work with whichever version you like best. I think both scripts do the job well. Using nested find_entity_views(): #!/usr/bin/perl -w use strict; use warnings; use VMware::VIRuntime; my %opts = ( hostname => { type => "=s", variable => "HOSTNAME", help => "Name of managed ESX host", required => 1, } ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my ($host_views, $datacenter, $hostname); $hostname = Opts::get_option('hostname'); my $host = Vim::find_entity_view( view_type => 'HostSystem', filter => { name => $hostname }, ); unless (defined $host) { Util::disconnect(); die "No host matching '$hostname' found.\n"; } my $datacenter_view = Vim::find_entity_views(view_type => 'Datacenter'); LOOP: foreach (@$datacenter_view) { $datacenter = $_->name; $host_views = Vim::find_entity_views( view_type => 'HostSystem', begin_entity => $_ ); foreach (@$host_views) { if ($_->name eq $host->name) { print "$datacenter\n"; last LOOP; } } } Util::disconnect(); Sample output: $time perl test.pl --server=172.16.14.51 --username=root --password=vmware --hostname=ESX-01.vmwlab.local ha-datacenter real 0m7.502s user 0m1.872s sys 0m0.078s Using ManagedObject parent property: #!/usr/bin/perl -w use strict; use warnings; use VMware::VIRuntime; my %opts = ( hostname => { type => "=s", variable => "HOSTNAME", help => "Name of managed ESX host", required => 1, } ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my $hostname = Opts::get_option('hostname'); my $entity_view; sub FindDatacenter { my ($entity) = shift; unless ( defined $entity ) { print "Root folder reached and datacenter not found!\n"; return; } if($entity->type eq "Datacenter") { my $datacenter_view = Vim::get_view(mo_ref => $entity, properties => \['name']); # remove the \ before the property list. print "Datacenter for HostSystem '" . $hostname . "' = " . $datacenter_view->name . "\n"; return; } $entity_view = Vim::get_view(mo_ref => $entity, properties => \['parent']); # remove the \ before the property list. FindDatacenter($entity_view->parent); } my $host = Vim::find_entity_view( view_type => 'HostSystem', filter => { name => $hostname }, ); unless (defined $host) { die "No host matching \'$hostname\' found.\n"; } FindDatacenter($host->parent); Util::disconnect(); Sample output: $time perl test2.pl --server=172.16.14.51 --username=root --password=vmware --hostname=ESX-01.vmwlab.local Datacenter for HostSystem 'ESX-01.vmwlab.local' = ha-datacenter real 0m4.326s user 0m1.429s sys 0m0.072s
You'll probably need to set FileQueryFlags: my $flags = new FileQueryFlags (fileSize => 1, fileType => 1, modification => 1); my $searchSpec = new HostDatastoreBrowserSearchSpec( details ... See more...
You'll probably need to set FileQueryFlags: my $flags = new FileQueryFlags (fileSize => 1, fileType => 1, modification => 1); my $searchSpec = new HostDatastoreBrowserSearchSpec( details => $flags ); You can set the booleans as needed.
Yes, the connectionState is an object (perl hash). Just call the 'val' property. + "Available for Management?: " . $_->summary->runtime->connectionState->val . "\n";+
Now that is just me being stupid. Sorry I don't have a VM environment to run the script against to catch some of the runtime errors. Thanks for bearing with me. I spelled guest as 'quest'. ... See more...
Now that is just me being stupid. Sorry I don't have a VM environment to run the script against to catch some of the runtime errors. Thanks for bearing with me. I spelled guest as 'quest'. Fixed and updated.
I haven't done this personally, but the UpdateVirtualSwitch function all on HostNetworkSystem via the SDK should allow you to do what you want. Of course changing the number of ports will requir... See more...
I haven't done this personally, but the UpdateVirtualSwitch function all on HostNetworkSystem via the SDK should allow you to do what you want. Of course changing the number of ports will require a reboot.
VIM2Runtime.pm does call VMware::VICommon. VIRuntime.pm calls VICommon. VILib is just the getopts supporting functions. Calling VIM2Runtime or VIM25Runtime is essentially asking for a particul... See more...
VIM2Runtime.pm does call VMware::VICommon. VIRuntime.pm calls VICommon. VILib is just the getopts supporting functions. Calling VIM2Runtime or VIM25Runtime is essentially asking for a particular SDK version directly. If you look deeper at the code in VICommon.pm, however, you'll notice it does in fact call VIMRuntime.pm and VIMStub.pm. unless ($vim_namespace) { $vim_namespace = "vim$server_version"; eval qq(require VMware::VIM${server_version}Runtime); die "$@\n" if $@; eval qq(require VMware::VIM$Stub); die "$@\n" if $@; } Since it's possible to get the VIM soap daemon version before login, the functions in VICommon determine what version to use (2, 2.5) and then add the appropriate runtime when you call your Login function. Unless you were really trying to bypass the general helper functions provided by the toolkit, none of this would require your coding attention when creating scripts.
Yes, toolStatus may not be defined (not set). This might be because the VM tools aren't installed. You have to check the value before you attempt to use it. If you're looping through Virtual M... See more...
Yes, toolStatus may not be defined (not set). This might be because the VM tools aren't installed. You have to check the value before you attempt to use it. If you're looping through Virtual Machines, you'll have to handle the case where it's not set. $toolsStatus = $vm->guest->toolsStatus ? $vm->guest->toolsStatus->val : ""
Did you add -DWITH_COOKIES to your compile flags? Not sure about the default gsoap behavior without it, but you might be getting 3 seperate HTTP sessions. First one for the Service Content, sec... See more...
Did you add -DWITH_COOKIES to your compile flags? Not sure about the default gsoap behavior without it, but you might be getting 3 seperate HTTP sessions. First one for the Service Content, second for the login and then the third one for the retrieve property call. If that's the case it would explain why you log in and then don't see anything from retrieve properties, you're in a new session that isn't authenticated.
Quick and dirty, but hopefully will get you moving on your script.