{"id":1211,"date":"2013-03-04T09:00:02","date_gmt":"2013-03-04T14:00:02","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=1211"},"modified":"2013-03-02T13:35:56","modified_gmt":"2013-03-02T18:35:56","slug":"get-vcenter-scheduled-tasks-with-powercli-part-1","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/get-vcenter-scheduled-tasks-with-powercli-part-1\/","title":{"rendered":"Get vCenter Scheduled Tasks with PowerCLI (Part 1)"},"content":{"rendered":"<p>I work with a lot of scheduled tasks.  Typically, these tasks run powershell scripts from the Windows Task Scheduler.  However, when working with simple VMware tasks (like creating snapshots right before a maintenance window) I like to use the Scheduled Tasks feature from right inside vCenter.  The other day I thought it would be interesting if I had a PowerShell function that I could run to create these snapshot tasks.  The first step in this process is to figure out what tasks we already have&#8230;and understand where some of these properties are located using PowerCLI.<\/p>\n<p>The following two functions can be used to get information on Scheduled Tasks inside vCenter.  The first function <strong>Get-VIScheduledTasks<\/strong> will simply return a list of all scheduled tasks that exist in a vCenter environment.  I picked out a handful of properties that I figured would be needed and return those by default, casting the date\/time values into local time.  If you need all of the properties (the native .Net view of the Scheduled Task Info objects) you can include the parameter <strong>-Full<\/strong> and all of the properties will be returned.<\/p>\n<pre><code class=\"language-powershell\">\r\nFunction Get-VIScheduledTasks {\r\nPARAM ( [switch]$Full )\r\nif ($Full) {\r\n  # Note: When returning the full View of each Scheduled Task, all date times are in UTC\r\n  (Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_).Info }\r\n} else {\r\n  # By default, lets only return common headers and convert all date\/times to local values\r\n  (Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_ -Property Info).Info } |\r\n  Select-Object Name, Description, Enabled, Notification, LastModifiedUser, State, Entity,\r\n    @{N=\"EntityName\";E={ (Get-View $_.Entity -Property Name).Name }},\r\n    @{N=\"LastModifiedTime\";E={$_.LastModifiedTime.ToLocalTime()}},\r\n    @{N=\"NextRunTime\";E={$_.NextRunTime.ToLocalTime()}},\r\n    @{N=\"PrevRunTime\";E={$_.LastModifiedTime.ToLocalTime()}}, \r\n    @{N=\"ActionName\";E={$_.Action.Name}}\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>This next function calls above function, but only returns the tasks whose action is &#8220;CreateSnapshot_Task&#8221;<\/p>\n<pre><code class=\"language-powershell\">\r\nFunction Get-VMScheduledSnapshots {\r\n  Get-VIScheduledTasks | ?{$_.ActionName -eq 'CreateSnapshot_Task'} |\r\n    Select-Object @{N=\"VMName\";E={$_.EntityName}}, Name, NextRunTime, Notification\r\n}\r\n<\/code><\/pre>\n<p>Sample usage would be:<\/p>\n<pre>\r\n# To find all tasks that failed to execute last run\r\nGet-VIScheduledTasks | ?{$_.State -ne 'success'}\r\n\r\n# To find all snapshots that are not scheduled to run again:\r\nGet-VMScheduledSnapshots | ?{$_.NextRunTime -eq $null}\r\n<\/pre>\n<p>I hope you find these functions useful.  Be sure to check out the next post &#8211; on creating scheduled snapshots with PowerCLI!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I work with a lot of scheduled tasks. Typically, these tasks run powershell scripts from the Windows Task Scheduler. However, when working with simple VMware tasks (like creating snapshots right before a maintenance window) I like to use the Scheduled &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/get-vcenter-scheduled-tasks-with-powercli-part-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":6,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1217,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions\/1217"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}