Reinicio automático de maquinas virtuales bloqueadas (vmware/VDI)

En ciertas ocasiones las VDI de algunos pools se nos quedan fritas, el so Windows del cliente se bloquea aparentemente por falta de recursos.
En el vmware vsphere client las vemos con alerta roja y lo que experiencia el usuario es que no puede acceder a su maquina.
Efectivamente le hacemos un ping y no responde. La solucion es reiniciar la maquina y con la ayuda de vmware powercli podemos automatizarlo.


VmWare Powercli es el snapin para gestionar el entorno virtual vmware desde cmdlets de powershell.



Con este script powershell conectamos con el Virtual Center que presenta los pools que queremos revisar (get-vm), le filtramos la salida para que solo saque las maquinas que estan arrancadas ($_.powerState -eq "PoweredOn") y les hacemos un ping. Si a pesar de estar arrancadas no responden al ping las reiniciamos con otro cmdlet de vmware powercli (restart-vm) y si responden al ping pero no responden a un get-service tambien las reiniciamos.
# Adds the base cmdlets
Add-PSSnapin VMware.VimAutomation.Core
. "C:\Archivos de programa\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

$ping = new-object System.Net.Networkinformation.Ping
$myVC = "ServerGBLD2"
$VCServer = Connect-VIserver -server $myVC -user VmVDIusr -password pas$word
$pool="desarrolloVisual","desarrolloCobol"
$pool|%{
$maquinas=get-vm -server $vcserver -location $_|?{$_.powerState -eq "PoweredOn"}
$maquinas|%{
 $strComputer=$_.name
 $valor=$ping.send($strComputer).STATUS
 if ($valor -eq "Success")
 {
 get-service -computername $strComputer|out-null
 if ($? -eq $false)
  {
  write-host $strComputer -fore yellow
  restart-vm -vm $strComputer -confirm:$false
  $ahora=get-date
  out-file ".\maquinasenrojo.log" -input "$ahora $strComputer reiniciada (GET-SERVICE)" -append
  }
  else
  {
  write-host $strComputer -fore green
  }
 }
 else
 {
 write-host $strComputer -fore red
 restart-vm -vm $strComputer -confirm:$false
 $ahora=get-date
 out-file ".\maquinasenrojo.log" -input "$ahora $strComputer reiniciada (PING)" -append
 } 
} #fin listado maquinas
}#fin de pools


Comentarios

Publicar un comentario