Killing me softly/hardly/forcely. How to kill a VM via #PowerCLI

Having observed some problems with VMs that were not able to be shutdown/powered-off properly via PowerCLI I tried to find a solution.

From time to time Shutdown-VMGuest didn’t worked and even an Stop-VM with the kill option were not working as expected. I knew that ESXTOP and ESXCLI have the options to kill a VM process/world if there are no other options. But since I wanted to achieve this in PowerCLI this blog post from the year 2011 gave me the correct hint.

We can use ESXCLI via PowerCLI to fulfil that task  😉 *whoopwhoop*.

I was missing a feature to kill those worlds without authenticating directly against an ESXi-host and since ESXCLI and it’s namespaces have changed a little within the last years I wanted to document now how this can be achieved in vSphere 5.X.

First of all connect to the vCenter via

Connect-VIServer $vCenterFQDN

Get the VM-Object you are going to kill

$vm = Get-VM $nameOfTheVM

find the ESXi-Host where the VM is running on

$esxi = Get-VMHost -VM $vm

and load the ESXCLI functionality

$esxcli = Get-EsxCli -VMhost $esxi

Now it’s time to extract the WorldID out of the ESXCLI VM PROCESS LIST data

$worldid = $esxcli.vm.process.list() | where{$_.Displayname -eq $hostname} | Select WorldID

and kill the VM with the options soft, hard, force

$esxcli.vm.process.kill("force",$worldid.WorldID)

VOILA the VM should be definitely killed right now. This ESXCLI commands is  not being tracked by the VPXA, so no events of the ‘kill’ are written down in the database. (With great knowledge comes great responsibility, right? ;-))

If you are running this command against a VM as part of an HA-Cluster. The HA-mechanism will reboot the VM after the kill. In this scenario you need to disable the HA-protection of the VM (so it is removed from the HA protected list) before you are going to kill it via.

$vm | Set-VM -HARestartPriority Disabled

I hope this information might be useful to some of you guys.

Please use the Code-Snippet here to see the fully-functional (Kill-VM.ps1) script.

Kill-VM.ps1

 

1 Comment

  1. Just what I needed
    Thank you very very much

Leave a Reply

Your email address will not be published.

*

CAPTCHA

*

© 2017 v(e)Xpertise

Theme by Anders NorenUp ↑