VMware Cloud Community
piercj2
Enthusiast
Enthusiast

Getting VM IP Address ?

Hi Guys,

I'm trying to get IP  Addresses associated with VM's.

I need to take into account the following situations

- VM has no IP Address

- VM has a single IP Address

- VM has multiple IP Addresses.

I need to Ping an IP Address to verify Network Connectivity. In the case of VM's with multiple IP's, I don't care if I can't ping all addresses (e.g. private IP's), I just need to ping one to prove some level of Network connection.

I'm using (Get-VM vmname).Guest.IPAddress.Count to get the total number of IP Addresses configured on a VM.

This is then used in a foreach loop to ping each IP, which is found using (Get-VM).Guest.IPAddress

for example:

$count = (Get-VM).Guest.IPAddress.Count

for ($I=1 ; $I -le $count+1 ; $I++) {

     blah blah blah

     $vmip = (Get-VM $vm).Guest.IPAddress

     Test-Connection $vmip -Quiet -Count 1

}

I've found in testing that sometimes (Get-VM vmname).Guest.IPAddress returns a null value.

I suspect that this is because VMware Tools isn't installed on all VMs.

If I'm right and this is a result of a lack of VMware Tools on particular VMs, is there any other way to get the VM's IP Address ?

Thanks

0 Kudos
2 Replies
LucD
Leadership
Leadership

Yes, that is due to missing VMware Tools.

This is a bit of a chicken-and-the-egg situation, you could launch a Get-WmiObject remotely against the guest, but that requires name resolution to function and that the guest is reachable over the network.

But that would already answer your original question :smileygrin:

Is installing the VMware Tools not an option ?


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

piercj2
Enthusiast
Enthusiast

Hi LucD,

Thanks again for the assist.

I've come up with the following to allow me test VM's. It's handy if you're Patching a number of Hosts and want a quick view of everything before and after the patching.

Given a list of ESXi Hosts in a Text file, the script will test connectivity to each host and will let you know if it's corresponding VM's are

- Powered Off, Suspended, Powered on

- If VMware Tools are installed, it'll also ping the VM

I'd like to improve the script by adding two extra features but, don't know how to do this

1) if Ping to a VM fails on an IPv4 address but, an IPv6 address is returns, I'd like for the script to handle this better. Currently it returns an error

2) I'd like to have the results output to a (.csv) file with 3 columns.

     - first column is Date and Time

     - Second column is name of Esxi Host

     - Third column is name of VM running on the Host

     - fourth column is Description of Current Status

Eg

Date & TimeESXi HostVMStatus
12 May 2015 09:00:00ESXi1Connected and replying to Pings on vmk0 - 10.10.10.1
12 May 2015 09:00:00ESXi1vm1PoweredOn and replying to Ping from IP Address 10.10.10.10
12 May 2015 09:00:00ESXi1vm2 PoweredOn and replying to Ping from IP Address 10.10.10.11
12 May 2015 09:02:00ESXi2Connected and replying to Pings on vmk1 - 11.11.11.1

$statusloaded=$false

$snapins=("VMware.VimAutomation.Core","VMware.VumAutomation")

foreach ($snapin in $snapins) {   

     Add-PSSnapin $snapin         

     if((Get-PSSnapin $snapin) –eq $null){   

      }   

     else {       

          $statusloaded=$true   

     }

}

Write-Host -BackgroundColor White -ForegroundColor Black "*************************************************************"

Write-Host -BackgroundColor White -ForegroundColor Black "*** Ensure you are logged into the correct vCenter Server ***"

Write-Host -BackgroundColor White -ForegroundColor Black "*************************************************************"

Write-Host ""

Connect-VIServer

Write-Host ""

Write-Host ""

Write-Host "*** The specified file must contain FQDN Nmaes of ESXi Hosts ***"

Write-Host "***            Enter one FQDN Name per line                  ***"

Write-Host ""

Write-Host ""

# Enter the filename that contains the list of ESXi Hosts

$filelocation = Read-Host "Please enter the Filename, including full path, of the target file"

# Places the ESXi Host names contained in the named file into a variable                                                                                                                                                        

$TargetHosts = Get-VMHost (Get-Content $filelocation)  

# Test connectivity to each ESXi Host                                                                                                        

foreach ($ESXiHost in $TargetHosts) {   

     $MgmtNic = Get-VMHost $ESXiHost | Get-VMHostNetworkAdapter | Where-Object {$_.ManagementTrafficEnabled}   

     $MgmtIP = $MgmtNic.IP       

     $TestHostConnection = Test-Connection $MgmtIP -Quiet -Count 1   

          if ($TestHostConnection -eq $true) {       

               $time = (Get-Date).ToString()       

               $hoststatus = (Get-VMHost $ESXiHost).ConnectionState       

               Write-Host ""       

               Write-Host $time "-- ESXi Host " $ESXiHost " is " $hoststatus " and replying to Pings on " $MgmtNic " - " $MgmtIP       

              

               # for each active ESXi Host, test connectivity to it's VM's       

               $TargetVMs = $ESXiHost | Get-VM       

               foreach ($vm in $TargetVMs) {                 

               # variable to hold Power State of VM's           

               $powerstate = (Get-VM $vm).PowerState           

               # variable to hold installation status of VMware Tools           

               $getview = Get-VM $vm | Get-View           

               $checkToolsStatus = $getview.Guest.ToolsStatus           

               # variable to hold IP Addresses configured on VM's           

               $vmip = (Get-VM $vm).Guest.IPAddress           

               # If VM Power State is anything other than Powered On (Powered Off, Suspended, etc...) let me know           

               if ($powerstate -ne "PoweredOn") {               

                    Write-Host -ForegroundColor Red "$time --      VM $vm is $powerstate"               

                    }           

               # If VMware Tools in not installed, let me know           

               elseif ($checkToolsStatus -eq "toolsNotInstalled") {               

                    Write-Host -ForegroundColor Cyan "$time --      VM $vm does not have VMware Tools Installed. Unable to get IP Address as a result"               

               }                                      

               # If the VM is Powered On and, VMware Tools are installed, test each of the VM's IP Addresses for connectivity           

               else {               

                    foreach ($ip in $vmip) {                   

                         $TestVMConnection = Test-Connection $ip -Quiet -Count 1                   

                         if ($TestVMConnection -eq $true) {                       

                              Write-Host "$time --      VM $vm is $powerstate and,is replying to Ping on Interface with IP Address $ip"                       

                              }                    

                         else {                       

                              Write-Host -ForegroundColor Red "$time --      VM $vm is $powerstate but, is not replying to Ping on Interface with IP Address $ip"

                              }

                    }

                }

              }

            }

    # Let me know if it's not possible to Ping the ESXi Host   

     else {        

          Write-Host -ForegroundColor Red "$time -- ESXi Host $ESXiHost is not replying to Ping, get to work!"

        }

}

Write-Host "" Read-Host "Press Enter to exit"

0 Kudos