VMWare Host QA PowerCLI Script

We were upgrading a bunch of ESXi hosts and needed to QA the guest VM’s after.  I wrote this PowerShell script to list the guest VM’s on a particular host, get all network adaptors/ IP addresses, and ping each and echo out the results.

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction silentlyContinue
$file = "\\FQDN\scripts$\VMWare\Logs\vHostQA-" + $(Get-Date -Format 'M_d_yyyy') + ".txt"
Connect-VIServer $vsphere

$VMhost = Read-Host "Enter the host name as displayed in vCenter"
$VMs = get-vm | Where {$_.VMhost -Like $VMhost}
ForEach ($vm in $VMs){
 $vmObj = get-vm -name $vm
 write-host "Checking: $vmObj"
 $PowerState = $vmObj.PowerState
 If ($PowerState -eq "PoweredON"){
 $NetworkAdapter = $vm.Guest.Nics
 ForEach ($adaptor in $NetworkAdapter){
 $NetworkName = $adaptor.NetworkName
 $connState = $adaptor.Connected
 ForEach ($IPAddress in $adaptor.IPAddress){
 If ($IPAddress.Contains(":")){
 #IP6
 }Else{
 $IP = $IPAddress
 }
 }
 $ping = New-Object System.Net.NetworkInformation.Ping
 try
 {
 $status = [string]($ping.Send($IP)).Status
 $address = [string]($ping.Send($IP)).Address
 }
 catch [System.Net.NetworkInformation.PingException]{$status = $null}
 switch($status){
 Success {Write-Host "$vm is up with IP: $address" -foregroundcolor Green
 $NewString = "$vm is up with IP: $address"
 $NewString | Out-File $file -Append -NoClobber -Encoding ASCII}
 Default {Write-Host "$vm not responding. IP: $IP Power State: $PowerState Network: $NetworkName Network Connected: $connState" -foregroundcolor RED
 $NewString = "$vm not responding. IP: $IP Power State: $PowerState Network: $NetworkName Network Connected: $connState"
 $NewString | Out-File $file -Append -NoClobber -Encoding ASCII}
 }
 }
 }else{
 write-host "$vm not responding. Power State: $PowerState" -foregroundcolor Red
 $NewString = "$vm not responding. Power State: $PowerState"
 $NewString | Out-File $file -Append -NoClobber -Encoding ASCII
 }
}

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