Restart Hung VM

In the lab during the week I came across a VM that was hanging intermittently. Being up the walls with other projects right now i decided to implement a script to check the VM intermittently and reset if it was hung until I get the chance to troubleshoot the issue. Added the following in a powershell script as a scheduled task in windows

# Load Windows PowerShell cmdlets for VMware
add-psSnapin VMWare* | out-null

# Connect To vCenter
Connect-VIServer -Server ServerName -User UserName -Pass Password | Out-Null

# Test Connection to the VM and reset if no response

$vm = Get-VM -Name VMName
if(!(Test-Connection -ComputerName VmHostName -Quiet))
{
Stop-VM -VM $vm -Confirm:$false -Kill
Start-VM -VM $vm
}

# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: