Wednesday 18 July 2012

Pinging NTP server IP from ESXi Shell using PowerShell

We can achieve this using Plink.exe utility. NOTE: root credentials are required to implement this using PS Script:

code is as below:

Connect-VIServer -Server 192.168.131.20 -Protocol https -user "vmadmin" -Pass "vmware@123"


$vmP = Get-VMHost |Sort Name
$str1="c:\plink.exe -v -l root "
$str9=' -pw "vmware@123"'
$str2="ping "
$str3="192.168.131.20"
$outfile="c:\rana.csv"

foreach($esxentry in $vmP){
$esxhost=$esxentry.name
$command=$str1+$esxhost+$str9+$str2+$str3
$esxentry.name >> $outfile
"Ping response from" +"$esxhost" + "to NTP Server" + "$str3" >>$outfile
$result=Invoke-Expression -Command $command
foreach($resultline in 1..$result.length){
$result[$resultline] >> $outfile
}
}


# script will check the ping response from each Hosts added to VC and output will be saved in c:\Rana.CSV
# you need to copy plink.exe utility @ C:\ from where youare triggering this script

========================
OutPut :

Host1.esxi.rana.com
Ping response from Host1.esxi.rana.com to NTP Server192.168.131.20

Host2.esxi.rana.com
Ping response from Host2.esxi.rana.com to NTP Server192.168.131.20
64 bytes from 192.168.131.20: icmp_seq=0 ttl=128 time=0.675 ms
64 bytes from 192.168.131.20: icmp_seq=1 ttl=128 time=0.494 ms
64 bytes from 192.168.131.20: icmp_seq=2 ttl=128 time=0.485 ms

--- 192.168.131.20 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.485/0.551/0.675 ms



in case of no ping response you will find out put like below:
 Host1.esxi.rana.com
Ping response from Host1.esxi.rana.com to NTP Server192.168.131.20


<BLANK>

 Host2.esxi.rana.com
Ping response from Host2.esxi.rana.com to NTP Server192.168.131.20
64 bytes from 192.168.131.20: icmp_seq=0 ttl=128 time=0.675 ms