PowerCLI Script to Ping Guest VMs by IP v4 address on ESXi

PowerCLI

PowerCLI 6.5.4 – A single ESXi host.  Seeking to view the IPV4 only address for .guest.IPaddress (not the IPV6 address) to ping.  Why?  If a VM name is web102-IIS, but the DNS entry is web102; you can not ping web102-IIS.

Trying to tune https://github.com/alanrenouf/PowerActions/blob/master/PingVMsOnHost.ps1 $computer = ($computer).guest.IPaddress to ping the IPV4 address (not IPV6).
The end result would be 1 ping attempt to all guest VMs on the host using the IPV4 address and write VMNAME 192.168.1.200 is pingable. VMNAME 192.168.1.205 is NOT pingable (in red)

Use case – create quick baseline of guest VM state before and after an ESXi host change.

<#
https://vmscribble.com PowerCLI 6.5.4 - Seeking to view the IPV4 only address for .guest.IPaddress (not the IPV6 address)
Get-VM | Select Name, @{N="IP";E={@($_.Guest.IPAddress)}}
Why? Trying to tune https://github.com/alanrenouf/PowerActions/blob/master/PingVMsOnHost.ps1 $computer = ($computer).guest.IPaddress to ping the IPV4 address (not IPV6). 
End result would be 1 ping attempt to all guest VMs on the host using the IPV4 address and write VMNAME 192.168.1.200 is pingable. VMNAME 192.168.1.205 is NOT pingable (in red)
#>

# Ask fo the vCenter and ESXi host name
$vCenter = Read-Host "Enter the FQDN of the vCenter"
$theesxihost = Read-Host "Enter the FQDN of the ESXi host"

# Connect to your vCenter
Connect-VIServer $vCenter | Out-Null

Function Ping-VMs-On-Host{

param
([Parameter(Mandatory=$true)][String]$hostname)

$hostEsx = Get-VMHost -Name $hostname
$vms = Get-VM -Location $hostEsx

foreach ($vm in $vms)
{
$ip = $vm.Guest.IPAddress[0]
if ($ip -ne $null){
if (Test-Connection $ip -count 1 -quiet) {write-host ($vm.Name + " with IP " + $ip + " pingable") -Foreground Green}
else {write-host ($vm.Name + " with IP " + $ip + " NOT pingable") -Foreground Red}
}
}
}

# The command
Ping-VMs-On-Host -hostname $theesxihost

# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false
The Ask

PowerCLI 6.5.4 – A single ESXi host.  Seeking to view the IPV4 only address for .guest.IPaddress (not the IPV6 address) to ping.  Why?  If a VM name is web102-IIS, but the DNS entry is web102; you can not ping web102-IIS.

Trying to tune https://github.com/alanrenouf/PowerActions/blob/master/PingVMsOnHost.ps1 $computer = ($computer).guest.IPaddress to ping the IPV4 address (not IPV6).
The end result would be 1 ping attempt to all guest VMs on the host using the IPV4 address and write VMNAME 192.168.1.200 is pingable. VMNAME 192.168.1.205 is NOT pingable (in red)

Use case – create quick baseline of guest VM state before and after an ESXi host change.

PowerCLI Script
<#
https://vmscribble.com PowerCLI 6.5.4 - Seeking to view the IPV4 only address for .guest.IPaddress (not the IPV6 address)
Get-VM | Select Name, @{N="IP";E={@($_.Guest.IPAddress)}}
Why? Trying to tune https://github.com/alanrenouf/PowerActions/blob/master/PingVMsOnHost.ps1 $computer = ($computer).guest.IPaddress to ping the IPV4 address (not IPV6). 
End result would be 1 ping attempt to all guest VMs on the host using the IPV4 address and write VMNAME 192.168.1.200 is pingable. VMNAME 192.168.1.205 is NOT pingable (in red)
#>

# Ask fo the vCenter and ESXi host name
$vCenter = Read-Host "Enter the FQDN of the vCenter"
$theesxihost = Read-Host "Enter the FQDN of the ESXi host"

# Connect to your vCenter
Connect-VIServer $vCenter | Out-Null

Function Ping-VMs-On-Host{

param
([Parameter(Mandatory=$true)][String]$hostname)

$hostEsx = Get-VMHost -Name $hostname
$vms = Get-VM -Location $hostEsx

foreach ($vm in $vms)
{
$ip = $vm.Guest.IPAddress[0]
if ($ip -ne $null){
if (Test-Connection $ip -count 1 -quiet) {write-host ($vm.Name + " with IP " + $ip + " pingable") -Foreground Green}
else {write-host ($vm.Name + " with IP " + $ip + " NOT pingable") -Foreground Red}
}
}
}

# The command
Ping-VMs-On-Host -hostname $theesxihost

# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false
Menu