PowerCLI

 View Only
  • 1.  Get Host FQDN and IP

    Posted Apr 28, 2011 07:03 PM

    trying to get fqdn and ip from a mixure of esxi 3.5/4.1 hosts



  • 2.  RE: Get Host FQDN and IP

    Posted Apr 28, 2011 07:27 PM

    Try this

    Get-VMHost | Select @{N="FQDN";E={$_.NetworkInfo.HostName + "." + $_.NetworkInfo.DomainName}},@{n="IP";E={$_.NetworkInfo.ConsoleNic[0].IP}}



  • 3.  RE: Get Host FQDN and IP

    Posted Apr 28, 2011 08:06 PM

    so i wanted to export to excel so i took your command and did the following:

    $viservers = "vCenter.net"
    foreach ($singleViserver in $viservers)
    {
    Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
    $HostReport = @()
    Get-VMHost |Get-View |%{
         $Report = "" | select Hostname, ip_address
         $Report.Hostname = $_.Name
         $Report.ip_address = $_.NetworkInfo.ConsoleNic[0].IP
         $HostReport += $Report
    }
    }
    $HostReport | Export-Csv ".\HostReport.csv" –NoTypeInformation

    but i was getting error messages about "cannot index into null array"

    so i ran your command and the IP column is coming back empty as well.



  • 4.  RE: Get Host FQDN and IP
    Best Answer

    Posted Apr 29, 2011 06:12 AM

    You do not need the Get-View cmdlet.

    Change this

    Get-VMHost |Get-View |%{

    to this

    Get-VMHost |%{

    The following version will support ESX and ESXi.

    $HostReport = @()
    
    $viservers = "vCenter.net"

    foreach
    ($singleViserver in $viservers) {     Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
        Get-VMHost |%{         $Report = "" | select Hostname, ip_address
           
    if($_.Extensiondata.Config.Product.ProductLineId -eq "embeddedEsx"){             $Report.Hostname = $_.Name             $Report.ip_address = $_.NetworkInfo.VirtualNic[0].IP         }         else{             $Report.Hostname = $_.Name             $Report.ip_address = $_.NetworkInfo.ConsoleNic[0].IP         }         $HostReport += $Report
        } }
    $HostReport | Export-Csv ".\HostReport.csv" –NoTypeInformation

    Note that I moved the initialisation of the $HostReport array outside the foreach loop.



  • 5.  RE: Get Host FQDN and IP

    Posted Apr 29, 2011 08:32 PM

    just for the sake of learning, i started out with a script that was similar but didn't work, but had in the past... just curious as to what changed. Also why did you move the host array outside of the loop?

    $viservers = "vCenter.net"
    foreach ($singleViserver in $viservers)
    {
         Connect-VIServer $singleViserver
    $HostReport = @()
    Get-VMHost |Get-View |%{
         $Report = "" | select Hostname, version, ip_address
         $Report.Hostname = $_.Name
         if($Report.version -like "3.5.*"){
         $Report.ip_address =$_.Config.Network.ConsoleVnic.Spec.ip.ipaddress
         }
         else {$Report.ip_address =$_.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress}
         $HostReport += $Report
    }
    }
    $HostReport | Export-Csv ".\Full-HostReport.csv" –NoTypeInformation



  • 6.  RE: Get Host FQDN and IP

    Posted Apr 29, 2011 08:37 PM

    If you initialise the $HostReport array inside the foreach loop, it will be initialised with each vCenter.

    Your CSV will only contain data for the last vCenter.