Pinging multiple VM’s: PowerCLI

This script might not be handy for everyone, but in specific cases it can help you quickly determine which VM’s are not responding on the network. This is a slight modification from my Windows 2008 Guest Networking: PowerCLI article.

#VM or naming convention to search for
$name = Read-Host "Please enter the VM or the naming convention to search for"
$location = Read-Host "Please enter the vCenter folder containing VM's you'd like to ping"

#Get the vCenter credentials, if there's not already a $Credential loaded from profile, etc
if(!$Credential){$Credential = Get-Credential}

#FQDN for vCenter server
$vcenter = Read-Host "FQDN of the vCenter server"

#Connecting to vCenter server
if(!$viserver){Connect-VIServer $vcenter -Credential $Credential}

$vms = Get-VM -Location $location | where {$_.Name -like "*$name*" -and $_.PowerState -eq "PoweredOn"}

foreach ($vm in $vms) {
	$ping = New-Object System.Net.NetworkInformation.Ping
	try
	{
		$status = [string]($ping.Send($vm)).Status
	}
	catch [System.Net.NetworkInformation.PingException]{$status = $null}
	switch($status){
		Success {}
		Default {
			Write-Host "$vm not responding."
		}
	}
}
if($viserver){Disconnect-VIServer $vcenter}

This script will find all powered on VM’s that match a specific naming convention, within a specific folder. It will then ping each one, and then output which ones are not responding. The catch in the foreach block is there to keep the script from blowing up if there’s no DNS record for a specific VM. It will report VM’s that are in DNS but not pinging, and VM’s that aren’t in DNS the same. It’s up to you to investigate after that. ๐Ÿ™‚

Comments

  1. gildas declercq says:

    good work
    very useful to make sure after a vmotion my VMs are still reachable

  2. I realize this is a very old post but am hoping it’s still being monitored somehow. Is there an easy way to output the content of this script to a file or Report? I apologize in advance for my absolutely limited scripting knowledge. Any help would be appreciated. Thx.

    • Hi Ron, probably the easiest way to save the output into a file would be to save the script above as a PowerShell script, a .ps1. You can do this through the PowerShell ISE, or a text editor. Once you’ve saved it, launch the PowerShell prompt, cd to where the saved script is located and use Out-File to capture the output of the script into a file.

      Example: PS C:\> ./pingscript.ps1 | out-file -filepath c:\pingscript_output.txt

      Hope this helps, and thanks for commenting!
      D

Speak Your Mind

*