VMware Cloud Community
marco2601
Contributor
Contributor

Ping VM on Host Scrip

I have the following script where it checks if I have ping to an IP of a VM, if it has ping it simply shows me a message that the VM has connection otherwise it deactivates the network interface of the VM aora the inconvenience is that it only validates once the ping, how can I make it validate the ping all the time until it tells me that it lost connection and sends the statement that disconnects the interface of the VM

Thank you for you help

+++++++++++++++++++++++++++++++

##Variables

$vcenter_server ="172.16.6.10"

$vcenter_user ="administrator@taurus.local"

$vcenter_pwd ="T4urus.2018.v1Center"

$VMIP= "172.16.6.100"

##Connect to vCenter

connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd

$results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$VMIP'"

if ($results.StatusCode -eq 0) {

Write-Host "$VMIP hostname is pingable"

}

else {

##Desconectar vNIC de la VM origen

Get-VM -Name AD-LAB | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false -Confirm:$false

##

}

0 Kudos
1 Reply
LucD
Leadership
Leadership

Not sure if I get the question 100%, but if you want to wait in a loop till the connection drops, you can do something like this

while (Test-Connection -ComputerName $vmip -Count 1 -Quiet) {

  sleep 5

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos