With the release of vSphere 5.0 there have been some changes to ESXCLI to bring the fragmented commands into more of a formal and discoverable structure as part of this ESXCLI has been expanded, as a reminder ESXCLI can be used from both vCLI and also in technical support mode, a high level of the new ESXCLI 5.0 name spaces as can be seen below:
Kill a VM with ESXCLI
As you can see from above, the structure has clearly changed from VSphere 4.1, lets take a look at an example, if we wanted to kill a stuck VM which has been documented as part of KB Article 1004340 which can be found here.
We can do this by using the following ESXCLI 5.0 example:
Firstly lets list the VMs running on the host which I am connected to:
Now we have this list we can kill the VM using the following options:
So lets kill our VM1 with a soft kill:
There is no screen output and no tasks registered in my vCenter server but I can assure you the VM just died !
Kill a VM with PowerCLI
With PowerCLI version 4.1 VMware introduced a new cmdlet called Get-ESXCLI, this cmdlet allows us to replicate what can be done in ESXCLI and therefore we can do the above trick quite easily from within PowerCLI, and not for just 1 VM, what if we had 10 or 20 VMs which were stuck ?!
As I mentioned earlier, the structure of ESXCLI has changed from 4.1 to 5.0 so if you have a 4.1 infrastructure then you will need to adjust my code below to call the correct command, either that or look at this great blog post here which was pointed out to me during the recording of episode 26 of the Get-Scripting PowerShell Podcast.
If you do have 5.0 then you can use the advanced function I created below, the examples show how to kill both 1 VM or multiple VMs, it doesn’t even care which hosts these VMs are on:
PowerCLI Code
The below is the advanced function which can be used with vCenter 5.0 and ESXi 5.0 hosts:
Function Kill-VM { <# .SYNOPSIS Kills a Virtual Machine. .DESCRIPTION Kills a virtual machine at the lowest level, use when Stop-VM fails. .PARAMETER VM The Virtual Machine to Kill. .PARAMETER KillType The type of kill operation to attempt. There are three types of VM kills that can be attempted: [soft, hard, force]. Users should always attempt 'soft' kills first, which will give the VMX process a chance to shutdown cleanly (like kill or kill -SIGTERM). If that does not work move to 'hard' kills which will shutdown the process immediately (like kill -9 or kill -SIGKILL). 'force' should be used as a last resort attempt to kill the VM. If all three fail then a reboot is required. .EXAMPLE PS C:\> Kill-VM -VM (Get-VM VM1) -KillType soft .EXAMPLE PS C:\> Get-VM VM* | Kill-VM .EXAMPLE PS C:\> Get-VM VM* | Kill-VM -KillType hard #> param ( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] $VM, $KillType ) PROCESS { if ($VM.PowerState -eq "PoweredOff") { Write-Host "$($VM.Name) is already Powered Off" } Else { $esxcli = Get-EsxCli -vmhost ($VM.Host) $WorldID = ($esxcli.vm.process.list() | Where { $_.DisplayName -eq $VM.Name}).WorldID if (-not $KillType) { $KillType = "soft" } $result = $esxcli.vm.process.kill($KillType, $WorldID) if ($result -eq "true"){ Write-Host "$($VM.Name) killed via a $KillType kill" } Else { $result } } } }
Pingback: How to stop non-responsive VM with PowerCLI - Notes of a scripter
Hi Alan,
I have powergui installed but i didn’t really know how to execute it, my vcenter is running on vsphere 5.5, could you please let me know how ?
Many Thanks in Advance
This script just saved me a ton of time and worked perfectly to kill a VM that was hung. Another great script that saved my butt.
Thanks Alan.
Pingback: vSphere5: Zombie VM mit PowerCLI abschiessen | ElasticSky
Manish, I dropped you an email.
Please provide a feedback to the Kb I have written 1004340 to kill a stuck vm with both methods and if you permit I can do the same internally. Let me know thro email if that is OK with you.