VMware Cloud Community
zgrywusek
Contributor
Contributor

Script to shutdown or suspend VM's and then turn off Host

Hi, I have been looking for a script that would do the following:

- suspend all VM's residing on the host according to VM Startup and Shutdown predefined on the host

then

- turn the host off gracefully

I really need something right away, if someone helps me out I don't mind sending out $20 gift card. (for working script).

Reply
0 Kudos
25 Replies
LucD
Leadership
Leadership

Reply
0 Kudos
zgrywusek
Contributor
Contributor

LucD, thank you. I found this script which works to the extent that it shuts down all VM's but not the host. Does your script shutdown ESXi at the end? Is there also a way to make the script to suspend those machines vs. shutdown? What I really want is 

1. shutdown or suspend the virtual machines gracefully, no hard shutdowns

2. shutdown ESXi host

3. I need to be able to run this script from one of the machines on that ESXi host, so it needs to go down as the last one and then issue shutdown to host.

Script I found:

# list of hosts to process
$listofhosts = "IP or NAME OF ESXi"

#loop through each host
Foreach ($esxhost in $listofhosts)
{
$currentesxhost = get-vmhost $esxhost
Write-Host "Processing $currentesxhost"

#loop through each vm on host
Foreach ($VM in ($currentesxhost | Get-VM | where { $_.PowerState -eq "PoweredOn" }))
{
Write-Host "===================================================================="
Write-Host "Processing $vm"

# if this is a vm that is supposed to go down with the ship.
if ($vmstoleave -contains $vm)
{
Write-Host "I am $vm - I will go down with the ship"
}
else
{
Write-Host "Checking VMware Tools...."
$vminfo = get-view -Id $vm.ID
# If we have VMware tools installed
if ($vminfo.config.Tools.ToolsVersion -eq 0)
{
Write-Host "$vm doesn't have vmware tools installed, hard power this one"
# Hard Power Off
Stop-VM $vm -confirm:$false

}
else
{
write-host "I will attempt to shutdown $vm"
# Power off gracefully
$vmshutdown = $vm | shutdown-VMGuest -Confirm:$false
}
}
Write-Host "===================================================================="
}
Write-Host "Initiating host shutdown in 40 seconds"
sleep 40
#look for other vm's still powered on.

Foreach ($VM in ($currentesxhost | Get-VM | where { $_.PowerState -eq "PoweredOn" }))
{
Write-Host "===================================================================="
Write-Host "Processing $vm"
Stop-VM $vm -confirm:$false
Write-Host "===================================================================="
}

#Shut down the host
Sleep 20

Set-VMhost -VMhost $currentesxHost -State Maintenance
Sleep 15

$currentesxhost | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)}

}
Write-Host "Shutdown Complete"

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, that is the Set-VMHost and Stop-VMHost doing.

But where does the script you found handle your "VM Startup and Shutdown predefined on the host" requirement?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

LucD - script I found is not doing that, was just testing it and I liked it only because it had a line to leave one of the VM's on which was driving this script to shut down at the end and go down with host.

Can you modify your script to the extent to put:

- $vmstoleave = "NameOfVM"

* Also I've run your script from my computer, it shutdown all of VM's but did NOT shut down the host.

and only shut it down as the last VM because it will run this script and then finally shut down the ESXi server? Would it be OK if we chat via some type of IM?

Reply
0 Kudos
LucD
Leadership
Leadership

Do I understand correctly you only have 1 ESXi node?
Then the script can't shut down the ESXi node which runs the VM on which the script is running..
Sorry, but I really can't fix impossible :beaming_face_with_smiling_eyes:

No, I don't do chats.
Can you imagine how many hours a day I would be busy with chat sessions.
That is simply not realistic I'm afraid.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

Yes you are correct, but we have several single ESXi servers in our branches.

We have about 70 branches, each branch has 1 ESXi server.

So your script cannot be modified to gracefully shut down the one ESXi server after all VM's shutdown?

Sorry about the IM question I completely understand.

Reply
0 Kudos
LucD
Leadership
Leadership

Not really.
The only option is to run that script on another machine (which is not shut down as part of the ESXi shutdown).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

LucD that is what I did for testing as well. I've run your script from my Windows10 machine from powercli. It turn off all Vm's but did not shut down the host.

OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {IP}

State : ShuttingDown

I got this for all 4VM's but host did not turn off.

at the end i got this: WARNING: One or more VMs did not stop

but all of them stopped and shutdown.

Reply
0 Kudos
LucD
Leadership
Leadership

The script only tries a graceful shutdown, it will not force a stop (with Stop-VM).
If the VMware Tools are not installed on all VMs, then the graceful shutdown will not work for all VMs.
The warning is from the script when it discovers that 1 or more VMs are still powered on.

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

As I've mentioned before ALL VM's shutdown without issue. I am only speaking about the host (ESXi server) it does NOT shut down after all VM's are turned off.

Reply
0 Kudos
LucD
Leadership
Leadership

When the script is running on one of these VMs, the shutdown of the VM interrupts the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

I am running the script from my computer not one of the VM's residing on that host.

 

Reply
0 Kudos
LucD
Leadership
Leadership

Just noticed one line was incorrect.
Try with this change

if((Get-VM -Location $esx).where{$_.PowerState -eq 'poweredoff'}.Count -eq 0){


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

Posting the whole script once more including the output. I am logged onto esxi host directly, i see machines are nicely being shutdown but host remains ON.

$esxName = '10.11.155.121'

$esx = Get-VMHost -Name $esxName
$esx.ExtensionData.Config.AutoStart.PowerInfo |
ForEach-Object -Process {
New-Object -TypeName PSObject -Property @{
VM = (Get-View -Id $_.Key -Property Name).Name
Order = $_.StartOrder
}
} | Sort-Object -Property Order,Name -Descending |
ForEach-Object -Process {
$vm = Get-VM -Name $_.VM
Shutdown-VMGuest -VM $vm -Confirm:$false
while($vm.ExtensionData.Runtime.PowerState -eq 'poweredOn'){
sleep 2
$vm.ExtensionData.UpdateViewData('Runtime.PowerState')
}
}

if((Get-VM -Location $esx).where{$_.PowerState -eq 'poweredoff'}.Count -eq 0){
Set-VMHost -VMHost $esx -State Maintenance
while(-not $esx.ExtensionData.Runtime.inMaintenanceMode){
sleep 2
$esx.ExtensionData.UpdateViewData('Runtime.inMaintenanceMode')
}
Stop-VMHost -VMHost $esx -Confirm:$false
}
else{
Write-Warning "One or more VMs did not stop"
}

 

When script is run:

Name Port User
---- ---- ----
10.11.155.121 443 root

OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.110.240}
State : ShuttingDown
Disks : {Capacity:2144333824, FreeSpace:1983119360, Path:D:\, Capacity:63897071616, FreeSpace:19686383616,
Path:C:\}
HostName : BR110DC
Nics : {Network adapter 1:10.11.110.x}
ScreenDimensions : {Width=1280, Height=800}
VmId : VirtualMachine-2
VM : BR110DC
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/
VmName : BR110DC
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest


OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.155.133, fe80::d1a6:92a5:fa62:2999}
State : ShuttingDown
Disks : {Capacity:63898120192, FreeSpace:51471728640, Path:C:\}
HostName : Test01
Nics : {Network adapter 1:LAN}
ScreenDimensions : {Width=1920, Height=853}
VmId : VirtualMachine-5
VM : Test01
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/
VmName : Test01
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest


OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.110.100, 169.254.179.203}
State : ShuttingDown
Disks : {Capacity:2308838518784, FreeSpace:1053267234816, Path:D:\, Capacity:322119397376,
FreeSpace:278769627136, Path:E:\, Capacity:53159653376, FreeSpace:13294333952, Path:C:\}
HostName : BR110DFS.corp.kearnyfederalsavings.net
Nics : {Network adapter 1:10.11.110.x}
ScreenDimensions : {Width=1024, Height=768}
VmId : VirtualMachine-1
VM : BR110DFS
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/
VmName : BR110DFS
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest

WARNING: One or more VMs did not stop

So after this warning nothing happens to the host, it remains on.

Reply
0 Kudos
LucD
Leadership
Leadership

My bad, that 'poweredoff' should of course be 'poweredon'


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zgrywusek
Contributor
Contributor

That worked, thank you, is there a way to "suspend" them vs. power off and then power off the host?

Reply
0 Kudos
LucD
Leadership
Leadership

You can use Suspend-VM instead of Shutdown-VMGuest.
Replace

 

Shutdown-VMGuest -VM $vm -Confirm:$false

 

with

 

Suspend-VM -VM $vm -Confirm:$false

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

zgrywusek
Contributor
Contributor

BTW like I stated I can send $20 paypal or venmo, send me something i can sent to and you will get it.

Reply
0 Kudos
LucD
Leadership
Leadership

Thanks for the offer, but I do this as a community contribution.
No reward required :smiling_face_with_sunglasses:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference