epicurean
Contributor
Contributor

Script to shutdown all VMs and hosts in a cluster on Saturday evening, and start it up on Mon Morning

Hi,

I am a newbie and just wondering if such a script exists?

My vcenter is a windows based VM on one of the 3 hosts.

much thanks!

11 Replies
LucD
Leadership
Leadership

The stop/start will not be a big issue, there cmdlets for that.

But you will need to clarify a couple of things:

  • You need a scheduler. Can you for example use the Windows Task scheduler on the box where vCenter is installed
  • When an ESXi is powered down, you will either need to push the power on button, or do this via an Out Of Band card on the HW where the ESXi is running

Depending on the answers on those questions, your solution would look different.


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

0 Kudos
epicurean
Contributor
Contributor

Thank you for the prompt reply!

1) yes, I can use the task schedule in the windows 2012 VM which houses my venter. But it's my Powe chute NETWORK SHUTDOWN VM which is the first VM to start for now

2) I would like the script to require no human interaction if possible since it's a weekend event

Much appreciated for your inputs

0 Kudos
LucD
Leadership
Leadership

If you vCenter itself is a VM running under the same vCenter, that will indeed be difficult to use as the scheduler, since it will be shut down.

You'll have to look for another platform from which the scheduling can be done.

For 2) you will have to investigate if the ESXi node can be started (powered on) through an Out Of Band card (you'll have to ask your HW team who installed the HW on which the ESXi is running). If not, there will be human interaction involved.


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

0 Kudos
epicurean
Contributor
Contributor

@LucD,

I confirm tthat the esxi node can be started via the standalone PC that is outside the cluster. That standalone PC can be left on all the time

So powercli should be installed on that PC to start the script as well?

0 Kudos
LucD
Leadership
Leadership

Yes, that would be your "management" station.

From there you stop and start everything.


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

0 Kudos
epicurean
Contributor
Contributor

great.

Would you happen to have such a script for my particular needs?

thank you

0 Kudos
LucD
Leadership
Leadership

Besides that start of the ESXi nodes, which is HW dependent, these are quite simple scripts.
I'll check if I have a template for that.

Once the vCenter is stopped, the script will need to connect directly to the ESXi node on which the vCenter was running.
For that you need a local account (root) on that ESXi node.
Do you have that account and it's password?


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

0 Kudos
epicurean
Contributor
Contributor

Yes, I do have the root accounts for all the ESXI nodes.

much thanks!

0 Kudos
LucD
Leadership
Leadership

Try something like the following for the shutdown of the environment.

$vcName = 'MyVC'

$rootPswd = 'password'

# Stop every VM and ESXi, except the vCenter and the ESXi node hosting the vCenter

Connect-VIServer -Server $vcName

# Find the vCenter

$vcVm = Get-VM -Name $vcName

# Shut down all VM, except the vCenter

Get-VM -Server $vcName | where{$_.Name -ne $vcName} | Shutdown-VMGuest -Confirm:$false

while((Get-VM -Server $vcName | where{$_.PowerState -ne 'PoweredOff'}).Count -ne 0){

  sleep 5

}

# Shut down all ESXi, except the one hosting the vCenter

Get-VMHost -Server $vcName | where{(Get-VM -Location $_).Count -eq 0} | Stop-VMHost -Confirm:$false

while((Get-VMHost | where{(Get-VM -Location $_).Count -eq 0}).Count -ne 0){

  sleep 5

}

Disconnect-VIServer -Server $vcName -Confirm:$false

# Shut down the vCenter and the ESXi, hosting the vCenter

Connect-VIServer -Server $vcVm.VMHost.Name -User root -Password $rootPswd

Get-VM -Server $vcVm.VMHost.Name | Shutdown-VMGuest -Confirm:$false

while((Get-VM -Server $vcVm.VMHost.Name | where{$_.PowerState -ne 'PoweredOff'}).Count -ne 0){

  sleep 5

}

Get-VMHost -Name $vcVm.VMHost.Name | Stop-VMHost -Confirm:$false

Disconnect-VIServer -Server $vcVm.VMHost.Name -Confirm:$false -Force


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

epicurean
Contributor
Contributor

Thank you.

How exactly do you make this into a script , and how do you call up the script  at the scheduled time?

0 Kudos
LucD
Leadership
Leadership

Have a look at Re: how can run a script in powercli

It contains a link to a document that introduces PowerShell and PowerCLI to beginners.

To schedule a PowerShell script, have a look at Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts


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