PowerCLI

 View Only
Expand all | Collapse all

Script to list VMs, IP, PowerState and Host details

  • 1.  Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 03:30 AM

    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



  • 2.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 07:24 AM

    Do your VMs all have the VMware Tools installed?

    And what do you mean by "Host details"?



  • 3.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 10:25 AM

    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



  • 4.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 10:36 AM

    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?



  • 5.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 12:55 PM

    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.  



  • 6.  RE: Script to list VMs, IP, PowerState and Host details
    Best Answer

    Posted Feb 09, 2018 01:05 PM

    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



  • 7.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 09, 2018 05:58 PM

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



  • 8.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 03:09 AM

    Need one more help.

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



  • 9.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 08:27 AM

    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

     



  • 10.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 09:56 AM

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



  • 11.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 10:11 AM

    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



  • 12.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 10:48 AM

    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



  • 13.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 10, 2018 12:27 PM

    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.



  • 14.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 13, 2018 05:25 PM

    Thank you for the update!

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



  • 15.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Feb 13, 2018 05:53 PM

    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



  • 16.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Sep 24, 2018 03:10 PM

    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



  • 17.  RE: Script to list VMs, IP, PowerState and Host details

    Posted Sep 24, 2018 03:39 PM

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