Categories
How-To Personal Software Technical

Power Off & On VMware Guest with a Scheduled Task

 

Using Windows task scheduler you can schedule power off and on events for guest systems running in VMware vCenter or a standalone ESXi host.

My steps:

  1. Create a basic task – give it a name and description (optional)
  2. Choose when you want this task to stat
  3. Select the start date and time
  4. Choose “Start a program”
  5. Choose the program you would like to run.  In this setup we will be running the following:
  6. C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File “C:\work\task\jermsmit.ps1”
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\work\task\jermsmit.ps1"

     

  7. Click Next, Select Yes when Task Scheduler prompts you
  8. On the Finish screen, click Finish  – You can open properties to set this to run unattended

 

The script I am now using does the following:

  1. Loads the VMware PowerCli modules to powershell
  2. Connects to Specified ESXi or vCenter Server
  3. Issues a stop to specific VM Guests
  4. Issues a start to the VM Guest

Script Example:

# 2015-04-22
# Power Off, Power On Powershell/PowerCli Script
# By: jermsmit - @jermsmit

#  Add all installed VMware cmdlests
Add-PSSnapin Vmware*

# Connects to Specified ESXi Server Host
Connect-VIServer -Server 10.255.12.6 -User root -Password adfdssssf!

# Issue Stop to Virtual Machine Guest
# Example Forceful Stop: Stop-VM -VM "web" -Kill -Confirm:$false
Stop-VM -VM "torproxy" -Confirm:$false
Stop-VM -VM "minecraft-vanilla" -Confirm:$false
Stop-VM -VM "web" -Confirm:$false

# Sleeps for 60 Seconds
Start-Sleep -Seconds 60

# Issue Start to Virtual Machine Guest
Start-VM -VM "torproxy" -Confirm:$false
Start-VM -VM "minecraft-vanilla" -Confirm:$false
Start-VM -VM "web" -Confirm:$false

 

Use cases:

  1. Powering systems down to conserve energy (earth day initiative)
  2. Allow for systems with large workloads to have full system resources without contention during scheduled down periods of the systems that are offline.
  3. Quick restore of nonpersistant environments

 

Thanks for visiting – jermal

5 replies on “Power Off & On VMware Guest with a Scheduled Task”

Many thanks Jermal.
I have been hunting high and low for some info like this. I have trawled the VMWare site and many many forums but none of them had any info that would help me write a script to shutdown our VM’s gracefully and in a set order. But what was even more beneficial to me, was the simplicity of your script. We have 3 hosts and 30 guests in our HA cluster and many of the VM’s are web and SQL servers, which need to be restarted in a specific order. Your script gave me the basis of what I have now tested and proved to be just what we needed to manage the shutdowns when our mains power fails and the batteries in the UPS reach our maximum shutdown threshold.
Best regards
Andy

Andy, thank you for the comment. Yes this is a ‘simple’ script to do a specific task. I’ve haven’t had a need to go back and modify it and it works for a demo environment flawlessly.

If you care to share what your script, and how your using it, I’m sure it will go a long way in helping others; including myself develop new methods.

Warm regards,

Jermal

Thanks for the post this helped us…Do you also have the similar scripts for the following steps: “shutting down” the VM and “delete it from the disk” and “clone to virtual machine” by specifying the VM name, selecting the host, selecting the virtual disk format and Finish.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.