VMware Cloud Community
thanosazlin
Contributor
Contributor

vSphere CLI command to get list of all esx hosts vcenter host manages?

i am tring to find a vsphere cli command, i have to stay away from vsphere powercli, that will query the vcenter windows host i am on and return to me a list of all the esx hosts that the vcenter host manages? and also if possible the version of each esx host?

thanks

Tags (1)
Reply
0 Kudos
9 Replies
chriswahl
Virtuoso
Virtuoso

Connect-VIServer vcenter.server.name

Get-VMHost | Select Name,Version,Build
VCDX #104 (DCV, NV) ஃ WahlNetwork.com ஃ @ChrisWahl ஃ Author, Networking for VMware Administrators
Reply
0 Kudos
thanosazlin
Contributor
Contributor

thank you but i mentioned that i did NOT want to use power cli, i need a vsphere cli command like a esxcfg or vicfg command from the vsphere cli command set that will work... i know i could do it with power cli but i have reasons why i can't use it for what i am trying to do.

if there is not a way please let me know. i assume i could attempt to query the sql dbase that vcenter uses some how. if i don't get any responses by sometime next week i'll likely look into that.

Reply
0 Kudos
chriswahl
Virtuoso
Virtuoso

Ah gotcha. No clue how to do that with a cli command in vsphere, not aware of any esxcfg- command that will do that.

Good luck. Smiley Happy

VCDX #104 (DCV, NV) ஃ WahlNetwork.com ஃ @ChrisWahl ஃ Author, Networking for VMware Administrators
Reply
0 Kudos
lamw
Community Manager
Community Manager

There is not a canned esxcfg-* script from VMware to do such since esxcfg-* commands are generally meant for configurations for an ESX(i) host but nothing stops you from using the vSphere API which is what esxcfg-* commands are based of using vSphere SDK for Perl (perl scripts) to query the API.

Here is a script that does exactly that and can run on a system that has vCLI installed or on vMA:

#!/usr/bin/perl -w

use strict;
use warnings;
use VMware::VILib;
use VMware::VIRuntime;

# validate options, and connect to the server
Opts::parse();
Opts::validate();
Util::connect();

my $vmhosts = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name']);

print "ESX(i) hosts residing on " . Opts::get_option('server') . "\n";
foreach(@$vmhosts) {
        print "\t" . $_->{'name'} . "\n";
}

Util::disconnect();

Here is an example of running the script:

[vi-admin@tancredi ~]$ ./getHosts.pl --server reflex --username primp
ESX(i) hosts residing on reflex
        vesxi41-2.primp-industries.com
        himalaya.primp-industries.com

It'll display the vCenter server and the attached ESX(i) host with the display names

Reply
0 Kudos
thanosazlin
Contributor
Contributor

@ Iamw , thanks so much that worked great, i don't know perl so don't have the time so thanks so much.

one more thing if i may please, can you provide me with perl code to also retrieve the version of ESX for the host, i assume after 'name' there would be some option to print 'version' or something to that like??? thanks in advance.

Reply
0 Kudos
lamw
Community Manager
Community Manager

Here's the updated script to include the version of the host:

#!/usr/bin/perl -w

use strict;
use warnings;
use VMware::VILib;
use VMware::VIRuntime;

# validate options, and connect to the server
Opts::parse();
Opts::validate();
Util::connect();

my $vmhosts = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name','summary']);

print "ESX(i) hosts residing on " . Opts::get_option('server') . "\n";
foreach(@$vmhosts) {
        print "\t" . $_->{'name'} . "\t" . $_->{'summary'}->config->product->fullName . "\n";
}

Util::disconnect();

Here is an example:

[vi-admin@tancredi ~]$ ./getHosts.pl --server reflex --username primp
ESX(i) hosts residing on reflex
        vesxi41-2.primp-industries.com  VMware ESXi 4.1.0 build-348481
        himalaya.primp-industries.com   VMware ESXi 4.1.0 build-348481

The script is Perl but you need to know more than just that, you also need to understand the vSphere API to extract the information you want. Basically anything you can do or see from the vSphere Client you can automate using various VMware SDK's.

Here are some resources if you're interested to give it a try:

http://www.vmware.com/support/developer/viperltoolkit/

http://www.vmware.com/support/developer/viperltoolkit/viperl41/doc/vsp_4_41_vsperl_proggd.pdf

http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html

Reply
0 Kudos
thanosazlin
Contributor
Contributor

thank you thank you thank you

Reply
0 Kudos
Piyush201110141
Contributor
Contributor

Do anyone have the idea how to get the host information which is managed by the Vcenter in Java . 

Reply
0 Kudos
gopinathan
Contributor
Contributor

Hi William, With vSphere 5.5, I am trying to pass this script to get the host detail programatically. The script do not accept @ in user@domain for the credentials. Is there a way to pass the @ from vCLI? 

Reply
0 Kudos