{"id":624,"date":"2021-02-23T18:44:00","date_gmt":"2021-02-23T18:44:00","guid":{"rendered":"http:\/\/192.168.8.14\/?p=624"},"modified":"2021-02-23T18:44:00","modified_gmt":"2021-02-23T18:44:00","slug":"vmware-tasks","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=624","title":{"rendered":"VMWare tasks"},"content":{"rendered":"\n<p>Some thing I always send a lot of time doing is looking for old tasks in vCenter. When using the web client or even Powershell you get a list of active tasks and some old ones before they disappear. You can go back through the Event list but sometimes you need to see the tasks.<\/p>\n\n\n\n<p>So here is a handy little bit of Powershell to do just that.<\/p>\n\n\n\n<p>Using the TaskCollector you can.<\/p>\n\n\n\n<p>First I create a TaskFilter object with a start and end time.<\/p>\n\n\n\n<p>Then I call the collector and get the first $tasknumber of tasks. then keep looping until I have all the tasks in my search window.<\/p>\n\n\n\n<p>Then I am, getting events for the same time window. I am saving to CSV nut more importantly I have both arrays in memory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$Olddays = 14 \n$NewDays = 10\n$tasknumber = 999 \n\n# created needed objects\n$serviceInstance = get-view ServiceInstance \n$taskMgr = Get-View TaskManager \n$TaskList = @() \n\n# create filter object\n$filter = New-Object VMware.Vim.TaskFilterSpec \n$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime \n$filter.Time.beginTime = (Get-Date).AddDays(-$Olddays)  # oldest\n$filter.Time.endTime = (Get-Date).AddDays(-$Newdays)    # newest\n$filter.Time.timeType = &quot;startedTime&quot; \n\n# read the first $tasknumber then keep getting more chunks of $tasknumber untill we run out of tasks\n$collectionImpl = Get-View ($taskMgr.CreateCollectorForTasks($filter)) \n$dummy = $collectionImpl.RewindCollector \n$collection = $collectionImpl.ReadNextTasks($tasknumber) \nwhile($collection -ne $null){ \n   $TaskList += $collection\n   $collection = $collectionImpl.ReadNextTasks($tasknumber) \n} \n\n# get events for the same time\n$events = get-vievent -start (get-date).AddDays(-$Olddays) -Finish (get-date).AddDays(-$Newdays) -MaxSamples 1000\n\n# save events and tasks to CSV\n$TaskList | Export-Csv &quot;C:\\temp\\Task-Export.csv&quot; -noTypeInformation\n$events | Export-Csv &quot;C:\\temp\\Event-Export.csv&quot; -noTypeInformation\n<\/pre><\/div>\n\n\n<p>After running the above I have 2 arrays; $TaskList and $Events for my time window, defined in the search filter object.<\/p>\n\n\n\n<p>Now I can query those arrays.<\/p>\n\n\n\n<p>Below are common querys I normally run.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n#what type of tasks I have\n$TaskList | select DescriptionId -Unique\n&lt;#\n  Examples of the DescriptionId for common tasks\n  Drm.ExecuteVMotionLRO                                      \n  Drm.ExecuteVmPowerOnLRO                                    \n  Datacenter.powerOnVm                                       \n  VirtualMachine.powerOff                                    \n#&gt;\n\n# list all tasks with desc, time etc\n$TaskList | select DescriptionId,StartTime,CompleteTime,State,EventChainId,Task | ft\n\n# list tasks of a certain type\n$TaskList | where{$_.DescriptionId -eq &quot;Datacenter.powerOnVm&quot;}\n\n\n# get events assoceated with a task\n$events | where{$_.chainid -eq ($TaskList | where{$_.key -eq &quot;task-135432&quot;}).eventchainid}\n\n# list all events for a task type\nforeach ($ChainID in ($TaskList | where{$_.DescriptionId -eq &quot;Drm.ExecuteVMotionLRO&quot;}).eventchainid){$events | where{$_.chainid -eq $ChainID} | select createdtime,chainid, fullformattedmessage}\n\n<\/pre><\/div>\n\n\n<p>So if something happened last week. Get the Events and tasks in to memory and you can searching for evidence.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some thing I always send a lot of time doing is looking for old tasks in vCenter. When using the web client or even Powershell you get a list of active tasks and some old ones before they disappear. You can go back through the Event list but sometimes you need to see the tasks&#8230;.<\/p>\n","protected":false},"author":1,"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":[5,4],"tags":[118,13,119,117,20],"class_list":["post-624","post","type-post","status-publish","format-standard","hentry","category-powercli","category-powershell","tag-event","tag-powershell","tag-search","tag-task","tag-vcenter"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=624"}],"version-history":[{"count":2,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":752,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions\/752"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}