vmvn01
Contributor
Contributor

shutdown vmguest

Hi,

I am using powershell + powerCLI to build a shutdown vmwares script. My goal is: using shutdown-vmguest command to shutdown the virtual machines, if VM's vmware tools have some problems (not running / not installed) switch to stop-vm command to power off virtual machines automatically.

I don't know how to monitor the command exit codes to branch to stop-vm command automation in the script yet. Please teach me about that case.

Thanks very much.

6 Replies
schepp
Leadership
Leadership

Hi,

I'd just do it in two foreach loops.

In the first loop use shutdown-VMGuest against all VMs that are poweredOn and have the VMware Tools running.

In the second loop use Stop-VM against all VMs that are running. That will kill your VMs without the Tools and also those with tools that hung up during shutdown.

Regards

vmvn01
Contributor
Contributor

Thank schepp for your quick reply ! Smiley Happy

your idea is quite helpful for me. But I am wondering about the time to finish the task of the script beacuse now we have hundreds of virtual machines need to shutdown in the short-time (about 15 minutes) in fact. So, how to run shutdown command to multiple virtual machine at the same time (parallel summited)? and according to your method (2 loops) when the Shutdown-VMguest loop finish, we have to wait for all virtual machines shutted down already (status to power off) then do the second loop use Stop-VM command for the other running VMs, right? if that it take me along time to done.

Thus, I want to Power off action (Stop-VM) with shutdown guest error VMs only for time saving.

Thanks

0 Kudos
schepp
Leadership
Leadership

Hey,

Ok, hundreds of VMs, you didn't mention that :smileygrin:

Good news: The Stop-VM cmdlet has a -RunAsync switch, so with this activated it won't wait for the task to complete and will stop all VMs in the loop very quickly: Stop-VM - vSphere PowerCLI Cmdlets Reference

Bad news: Shutdown-VMGuest doesn't have that switch.

As a Workaround I would think about launching one instance of the script for each of your ESXi server ( depending on the number of VMs running on each ).

For example you could pass the name of your ESXi hosts as an argument to the script like .\shutdown.ps1 esxi5.domain.local

and let each instance of the script only shutdown the VMs that are running on this one ESXi server:

Get-VMHost $argument | Get-VM

Regards

Tim

vmvn01
Contributor
Contributor

Tim,

yeah, exactly 490 VMs are running on 70 ESXi hosts in our environment now :smileygrin:. As your above workaround about seperate instances (sub-scripts) to shutdown VMs on each ESXi host, I understand that maximum VMs shutting down at the same time are 70? Wow, sound good for me ! :smileygrin: but that is ideal condition because we have not balance the VMs amount on each ESXi hosts that depend on VMs resources loaded and Cluster DRS sensitive configurations.

Well, I will try to do as your comments.

Thanks for your sound Smiley Happy

0 Kudos
schepp
Leadership
Leadership

vmvn01 wrote:

I understand that maximum VMs shutting down at the same time are 70? Wow, sound good for me ! :smileygrin: but that is ideal condition because we have not balance the VMs amount on each ESXi hosts that depend on VMs resources loaded and Cluster DRS sensitive configurations.

When you run the script once for every Host, yes you will get the 70 shutdowns simultaneously Smiley Wink Keep in mind there's a 500 sessions limit in vCenter, in case you have manye other things running. Also I have no idea how 70 running scripts will impact the performance. Might be usefull to reduce the number of running scripts a little by processing 2 or 3 ESXi hosts per script instance Smiley Wink

Well, I will try to do as your comments.

I hope you don't try it in your productive environment :smileygrin:

Regards

0 Kudos
vmvn01
Contributor
Contributor

haha, testing evironment only Tim :smileygrin:. We have a lot of Hosts but divide them into some clusters, I will test with cluster simultaneously level Smiley Wink

Thanks./.

0 Kudos