shankamu
Enthusiast
Enthusiast

Script to list VMs, IP, PowerState and Host details

Jump to solution

Hi All, Goodday!

I am looking for a script to list VMs, IP, PowerState and Host details for 1700 stand alone ESXi host. Could you please and guide for the script.

Thank you.

Regards

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Assuming the CSV content looks like this

ESXName

esx1

esx2

Try something like this

$user = 'root'

$pswd = 'VMware1!'

$report = @()

Import-Csv -Path vmhost-names.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $report += (Get-VM -Server $srv |

    Select Name,PowerState,

        @{N='VMHost';E={$_.VMHost.Name}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}})

   

    Disconnect-VIServer -Server $rv -Confirm:$false

}

$report | Export-Csv -Path report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

16 Replies
LucD
Leadership
Leadership

Do your VMs all have the VMware Tools installed?

And what do you mean by "Host details"?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shankamu
Enthusiast
Enthusiast

Yes, All VMs have VM Tools installed.

I mean VMs name, VMs IP, VMs PowerState and Host Name. I want to collect these information for 1700 ESXi stand alone host and they have common password for time being.

Regards

0 Kudos
LucD
Leadership
Leadership

Are these ESXi hosts all added to a vCenter?
Then you could connect to all vCenters, and do

Get-VM |

Select Name,PowerState,

    @{N='VMHost';E={$_.VMHost.Name}},

    @{N='IP';E={$_.Guest.IPAddress -join '|'}}

If they are not in a vCenter, then you would need a list somewhere (CSV ?) of the hostnames of all the ESXi nodes.
Is there such a list?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shankamu
Enthusiast
Enthusiast

Yes, I have all the list of host names in csv file. Some are added to vCenter and some not. Hence I would like go by calling csv file and i will include credentials in the script as they are common for now.  

0 Kudos
LucD
Leadership
Leadership

Assuming the CSV content looks like this

ESXName

esx1

esx2

Try something like this

$user = 'root'

$pswd = 'VMware1!'

$report = @()

Import-Csv -Path vmhost-names.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $report += (Get-VM -Server $srv |

    Select Name,PowerState,

        @{N='VMHost';E={$_.VMHost.Name}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}})

   

    Disconnect-VIServer -Server $rv -Confirm:$false

}

$report | Export-Csv -Path report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

shankamu
Enthusiast
Enthusiast

Thank you so much! Its working great and able to collect the information.

0 Kudos
shankamu
Enthusiast
Enthusiast

Need one more help.

Could you please share the code to collect the ESX host serial number for these 1700 hosts.

0 Kudos
LucD
Leadership
Leadership

Try like this

$user = 'root'

$pswd = 'VMware1!'

$report = @()

Import-Csv -Path vmhost-names.csv -UseCulture | %{

    $srv = Connect-VIServer -Server $_.ESXName -User $user -Password $pswd

    $report += (Get-VM -Server $srv |

    Select Name,PowerState,

        @{N='VMHost';E={$_.VMHost.Name}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}},

        @{N='VMHost Serial';E={((Get-EsxCli -VMHost $_.VMHost -V2).hardware.platform.get.Invoke()).SerialNumber}})

  

    Disconnect-VIServer -Server $rv -Confirm:$false

}

$report | Export-Csv -Path report.csv -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shankamu
Enthusiast
Enthusiast

I tried it, serial number is not capturing, just VMHost Serial number column is created in the report.

0 Kudos
LucD
Leadership
Leadership

And when you change that line with

        @{N='VMHost Serial';E={((Get-EsxCli -VMHost $_.VMHost).($esxcli.hardware.platform.get()).SerialNumber).SerialNumber}})

If it does return the serial in that form, it might be time look at upgrading your PowerCLI version.
Have a look at the version with Get-PowerCLIVersion


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shankamu
Enthusiast
Enthusiast

Yes, I changed that line while running previously. it did not return the serial number.

I have PowerCLI 5.5 release 1 build 1295336 installed.

Please advise if this version support or which version supports for these commands

0 Kudos
LucD
Leadership
Leadership

This kind of HW information, including the serial number, depends on the HW vendor.
Some vendors make this info available, others don't I'm afraid.

You could try to upgrade your PowerCLI version, but like I said that is no guarantee, it also depends on the HW vendor.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shankamu
Enthusiast
Enthusiast

Thank you for the update!

All servers Cisco and Dell servers. Is there any possibilities to just collect the serial number.

0 Kudos
LucD
Leadership
Leadership

I'm afraid I don't have access to such HW, so I can't try it out.

Some users seems to have success with $esx.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
szemmali
Contributor
Contributor

Hello everybody check my github repository and you will find 2 scripts:

1- Collect all VMs Info by vCenter with Login/Password 

2- Collect all VMs Info by vCenter using AD Account 

https://github.com/szemmali/vmware-collect.git

0 Kudos
LucD
Leadership
Leadership

Nice scripts, but what is the relation of those scripts to this thread?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos