Created
September 20, 2017 03:34
-
-
Save Zsoldier/a57662a3ad7a55bd6c2d567828d66d45 to your computer and use it in GitHub Desktop.
Example showing how to get/set scheduled tasks in vCenter/vSphere using PowerCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-vCenterScheduledTask{ | |
<# | |
.SYNOPSIS | |
Retrieve vCenter Scheduled Tasks. | |
.DESCRIPTION | |
Retrieve vCenter Scheduled Tasks. | |
.NOTES | |
Source: Automating vSphere Administration | |
Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd, | |
Alan Renouf, Glenn Sizemore | |
.EXAMPLE | |
Get-vCenterScheduledTask | Select-Object Name,Description,NextRunTime,PrevRunTime,State,Notification | |
#> | |
$si = Get-View ServiceInstance | |
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager | |
$tasks = $scheduledTaskManager.RetrieveEntityScheduledTask($null) | |
$scheduledtasks = foreach ($task in $tasks){(Get-View $task).info} | |
$scheduledtasks | |
} | |
<# | |
The above function demonstrates how to "GET" scheduled task information for pretty much EVERYTHING. It doesn't have a filter. | |
Might be a new version out there, but here are some other samples to filter or even reconfigure a scheduled task. | |
$vm = get-vm nameofVMIminterestedin | |
$si = Get-View ServiceInstance | |
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager | |
$tasks = $scheduledTaskManager.RetrieveObjectScheduleTask($vm.extensiondata.moref) # <-- you could replace $vm.extensiondata.moref w/ any valid object, such as an ESXi host. | |
$scheduledtasks = foreach ($task in $tasks){(Get-View $task).info} | |
$scheduledtasks | |
Example of how you could reconfigure an existing task using the return from the function above. | |
$vmsch = Get-vCenterScheduledTask | where {$_.Name -match "something completely idiotic so nothing bad happens by running this without knowing what they are doing"} | |
Foreach ($task in $vmsch) | |
{ | |
$spec = New-Object VMware.Vim.ScheduledTaskSpec | |
$spec.name = $task.name | |
$spec.description = $task.description | |
$spec.enabled = $true | |
$spec.scheduler = New-Object VMware.Vim.OnceTaskScheduler | |
$spec.scheduler.runAt = $task.scheduler.runAt | |
$spec.action = New-Object VMware.Vim.MethodAction | |
$spec.action.name = "CreateSnapshot_Task" | |
$spec.action.argument = New-Object VMware.Vim.MethodActionArgument[] (4) | |
$spec.action.argument[0] = New-Object VMware.Vim.MethodActionArgument | |
$spec.action.argument[0].value = "" | |
$spec.action.argument[1] = New-Object VMware.Vim.MethodActionArgument | |
$spec.action.argument[1].value = "" | |
$spec.action.argument[2] = New-Object VMware.Vim.MethodActionArgument | |
$spec.action.argument[2].value = $true | |
$spec.action.argument[3] = New-Object VMware.Vim.MethodActionArgument | |
$spec.action.argument[3].value = $false | |
$spec.notification = "" | |
$_this = Get-View -Id $task.ScheduledTask | |
$_this.ReconfigureScheduledTask($spec) | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment