Here is a PowerShell script I use that uses a custom function, it gives a VM 150 seconds to shut down before I force it off. All your really need to do is change the IP address to vcenter or ESXi host
. The stop-vmhost at the bottom shuts the esxi host off at the end.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
function Shutdown-MyVM
{
Param(
[Parameter(Mandatory=$true)]
[string] $vmname
)
$myvm=Get-VM $vmname
$i=0
while($myvm.PowerState -ne 'PoweredOff')
{
$ToolsStatus = ($myvm| Get-View).Guest.ToolsStatus
$ToolsStatus
$i
if($ToolsStatus -eq "toolsNotInstalled" -or $i -gt 5)
{
Write-Host "Stopping" $myvm.Name
Stop-VM -VM $myvm -Confirm:$false
}else{
Write-Host "Shutting down" $myvm.Name
Shutdown-VMGuest -VM $myvm -Confirm:$false
sleep 30
$i++
}
$myvm=Get-VM $vmname
if($myvm.PowerState -eq 'PoweredOff')
{
break;
}
}
}
Connect-VIServer 192.168.2.6 -User root -Password Password
Shutdown-MyVM -vmname esxi2.sjlab.net
Shutdown-MyVM -vmname esxi3.sjlab.net
Shutdown-MyVM -vmname file3.sjlab.net
Shutdown-MyVM -vmname file1.sjlab.net
Stop-VMHost -Server 192.168.2.6 -Confirm:$false -Force
Disconnect-VIServer * -Confirm:$false -Force