Save to My DOJO
Scouring through the various threads on VMware Technology Network (VMTN) forum, I came across a post on ways to create a scheduled task on vCenter Server. The owner of the thread was interested in learning how to schedule a VM to power up at a set time on a daily basis. The correct answer, of course, is to use vCenter Server’s inbuilt task scheduler, a feature available only to those who have vCenter deployed.
If you don’t have vCenter, there are other ways you can use to schedule tasks on ESXi. These include PowerShell, PowerCLI, and any guest OS’ inbuilt scheduler.
Let’s have a look at vCenter Server’s scheduler first.
Note: While the task scheduler is available in most versions of vCenter Server, this post is specific to vCenter Server 6.x.
Which Tasks Can You Schedule?
Here’s a list of all the tasks one can schedule depending on the vSphere object selected be it vCenter Server itself, a datacenter, an ESXi host or a virtual machine.
Scheduled Task | Description |
Add a host | Adds the host to the specified datacenter or cluster. |
Change the power state of a virtual machine | Powers on, powers off, suspends, or resets the state of the virtual machine. |
Change cluster power settings | Enable or disable DPM for hosts in a cluster. |
Change resource settings of a resource pool or virtual machine | Changes the following resource settings:
|
Check compliance of a profile | Checks that a host’s configuration matches the configuration specified in a host profile. |
Clone a virtual machine | Makes a clone of the virtual machine and places it on the specified host or cluster. |
Create a virtual machine | Creates a new virtual machine on the specified host. |
Deploy a virtual machine | Creates a new virtual machine from a template on the specified host or cluster. |
Migrate a virtual machine | Migrate a virtual machine to the specified host or datastore by using migration or migration with vMotion. |
Make a snapshot of a virtual machine | Captures the entire state of the virtual machine at the time the snapshot is taken. |
Scan for Updates | Scans templates, virtual machines, and hosts for available updates. This task is available only when vSphere Update Manager is installed. |
Remediate | Installs missing patches from the baselines selected for remediation on the hosts discovered during the scan operation and applies the newly configured settings. This task is available only when vSphere Update Manager is installed. |
Creating a Scheduled Task
For argument’s sake, let’s say you want to power up a VM at precisely 1 AM. Here’s how you would schedule it.
Step 1: Login vCenter Server using the vSphere Web client.
Step 2: Locate the VM and click on it.
Step 3: Click on the Monitor tab and select the Tasks & Events sub-tab as you do. Click on Scheduled Tasks and select a task from the Schedule a New Task drop-down menu as shown.
Step 4: Click on the Power On task. This brings up the scheduler window where you’ll do the following:
- Click on Scheduling options.
- Type a description for the task. This makes it easier to identify tasks and their purpose if you have multiple ones set up for the same object. That said, creating multiple tasks for the same object is not advised as it could lead to unpredictable results.
- Click on the Change icon.
- I want the VM to power up every day at 1 AM, so I choose the last option as shown in the next screenshot.
- Press OK to close the Configure Scheduler window.
- You can optionally specify an email address to get notified whenever the task executes. Press OK to save the task.
The full details for scheduling tasks can be viewed here.
Here’s another example. This time, I’m going to add a host to a datacenter using a scheduled task. To be honest, I don’t think this is too common a use-case but since the task is available I might as well try it. The video captures the entire process of setting it up. At the end of the video, you can see the event notification sent out to the mailbox.
Managing Scheduled Tasks
After you create a task, you can go back and edit it, run it on the spot or simply delete it. Clicking on the vCenter Server object in Navigator gives you an overview of all the scheduled tasks created including those that come packaged with components such as Update Manager.
On the Scheduled Tasks page, you will also find the history of every scheduled task ever run. To edit or remove a scheduled task, click on the respective icon or select the option from the context menu when right-clicking on the scheduled task.
If you click on the Run icon, the task executes irrespective of the set schedule. So going back to the previous power up at 1 AM example, the VM would power up immediately if I were to run the scheduled task manually.
TIP of the Day: Holding down the CTRL key and more!
I learned this trick just now while writing the post. Pull up the context menu on the currently selected object and press the CTRL key on your keyboard. A clock icon will be displayed next to any operation or task that supports scheduling. The next video demonstrates this. I’m simply holding down the CTRL key as I move from one context menu item to the next.
Last but not least, you can use PowerShell / PowerCLI for your scheduling needs. To date, there are no cmdlets that handle schedule tasks directly. Instead, you’ll need to code your way around the issue as greatly explained in this article.
You could also use something similar to the PowerCLI code shown below. This one-liner waits X amount of time before executing a specific command such as powering off a VM. A poor man’s scheduler if you will but nevertheless one that works.
do {start-sleep 1} until ((get-date) -ge (get-date "01:00")) ; Get-VM -name "Centos 64-bit" | Stop-VM -Confirm:$false
The line is basically a loop that waits until the current time matches that at which you want the task executed. The start-sleep bit, is a 1-second delay that keeps executing until the get-date condition is met. The loop is immediately followed by the command (task) you wish to execute which in this case powers off a VM called Centos 64-bit. Alternatively, use the Windows scheduler or a Linux Cron job to execute a script, or similar, to achieve a similar result.
Conclusion
We’ve seen that with vCenter Server, you can set scheduled tasks using the inbuilt task scheduler. If vCenter Server is not an option, you can use a combination of PowerShell and PowerCLI to script your way through as per the one-liner examples given.
[the_ad id=”4738″][the_ad id=”4796″]
Not a DOJO Member yet?
Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!