Automated Graceful shutdown in case of power outage
specs : vSphere 6.7 HP R5000 UPS
I am looking for a way to shutdown in a certain sequence not just mass shutdown
example vm1, vm2, vm3 and last your DC, then host, UPD etc
I have read a lot of the option, posted online .. wondering if anyone here in the community
might have a better option. Step by Step
one of the option Scripts I have found
freenasid=`vim-cmd vmsvc/getallvms | sed '1d' | awk '/Eds-FS.vmx/{print$1}'`
vmids=`vim-cmd vmsvc/getallvms | sed '1d' | awk '{print$1}'`
for vmid in $vmids
do
if [ $vmid != $freenasid ]
then
powerstate=`vim-cmd vmsvc/power.getstate $vmid | sed '1d'`
if [ "$powerstate" = "Powered on" ]
then
onvmids="$onvmids $vmid"
fi
fi
done
for vmid in $onvmids
do
vim-cmd vmsvc/power.shutdown $vmid
done
exit 0
I have heard it works, but does not solve the problem - I am looking for an answer to
the script is free for all, as the developer gave it out free to all and myself.
if someone has some experience in this that would be great
thanks in advance.
Single Host with only free vSphere Hypervisor license or vCenter around?
Regards,
Joerg
Vcenter / Cloud environment Multi Host
Than you can use Tags or Custom Attributes to place the information in which order or grouped order you wanna shutdown your VMs. These information can be fetched by a powershell/perl/python script. Same Script can use SNMP to fetch the UPS status. APC and Eaton have Virtual Appliances for this.
Regards,
Joerg
HP does not have a script as far as I can tell
APC has power chute, not compatible though
I have the R5000 HP UPS x2 , they are network ready now
via IPs
I need a template or something to do this with .. fill in the order and preferred
method ..
so for a powershell
$VMList = Get-Content c:\temp\vm.txt
foreach($vmName in $VMList){
$vm = Get-VM -Name $vmName
if($vm.Guest.State -eq "Running"){
Shutdown-VMGuest -VM $vm -Confirm:$false}
else{
Stop-VM -VM $vm -confirm:$false
write-host "Powering Down VM..."
Start-Sleep 20
write-host "Adjusting Compute..."
set-vm -numCPU 2 -memorygb 4 -confirm:$false
start-vm -confirm:$false
write-host "Powering On Server..."
}
}
how would I be sure things get entered into maintenance mode
before shutting down .. to avoid read/write corruption
what would you add to this to improve
Moderator: Thread moved to the PowerCLI area.
I would definitely add a loop where I check the PowerState of these VMs.
Just to make sure they are all powered off before I continue shutting down the ESXi nodes.
I do hope that the script doesn't run on one of those VMs
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I wrote an implementation for my home lab in PowerShell. Some things you'll need to think about and implement:
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
can this be moded to have shut down vm s from a text file in a certain order with maintenance mode