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 aft...
See more...
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 & Time ESXi Host VM Status 12 May 2015 09:00:00 ESXi1 Connected and replying to Pings on vmk0 - 10.10.10.1 12 May 2015 09:00:00 ESXi1 vm1 PoweredOn and replying to Ping from IP Address 10.10.10.10 12 May 2015 09:00:00 ESXi1 vm2 PoweredOn and replying to Ping from IP Address 10.10.10.11 12 May 2015 09:02:00 ESXi2 Connected 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"