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.
So here is a handy little bit of Powershell to do just that.
Using the TaskCollector you can.
First I create a TaskFilter object with a start and end time.
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.
Then I am, getting events for the same time window. I am saving to CSV nut more importantly I have both arrays in memory.
$Olddays = 14
$NewDays = 10
$tasknumber = 999
# created needed objects
$serviceInstance = get-view ServiceInstance
$taskMgr = Get-View TaskManager
$TaskList = @()
# create filter object
$filter = New-Object VMware.Vim.TaskFilterSpec
$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$filter.Time.beginTime = (Get-Date).AddDays(-$Olddays) # oldest
$filter.Time.endTime = (Get-Date).AddDays(-$Newdays) # newest
$filter.Time.timeType = "startedTime"
# read the first $tasknumber then keep getting more chunks of $tasknumber untill we run out of tasks
$collectionImpl = Get-View ($taskMgr.CreateCollectorForTasks($filter))
$dummy = $collectionImpl.RewindCollector
$collection = $collectionImpl.ReadNextTasks($tasknumber)
while($collection -ne $null){
$TaskList += $collection
$collection = $collectionImpl.ReadNextTasks($tasknumber)
}
# get events for the same time
$events = get-vievent -start (get-date).AddDays(-$Olddays) -Finish (get-date).AddDays(-$Newdays) -MaxSamples 1000
# save events and tasks to CSV
$TaskList | Export-Csv "C:\temp\Task-Export.csv" -noTypeInformation
$events | Export-Csv "C:\temp\Event-Export.csv" -noTypeInformation
After running the above I have 2 arrays; $TaskList and $Events for my time window, defined in the search filter object.
Now I can query those arrays.
Below are common querys I normally run.
#what type of tasks I have
$TaskList | select DescriptionId -Unique
<#
Examples of the DescriptionId for common tasks
Drm.ExecuteVMotionLRO
Drm.ExecuteVmPowerOnLRO
Datacenter.powerOnVm
VirtualMachine.powerOff
#>
# list all tasks with desc, time etc
$TaskList | select DescriptionId,StartTime,CompleteTime,State,EventChainId,Task | ft
# list tasks of a certain type
$TaskList | where{$_.DescriptionId -eq "Datacenter.powerOnVm"}
# get events assoceated with a task
$events | where{$_.chainid -eq ($TaskList | where{$_.key -eq "task-135432"}).eventchainid}
# list all events for a task type
foreach ($ChainID in ($TaskList | where{$_.DescriptionId -eq "Drm.ExecuteVMotionLRO"}).eventchainid){$events | where{$_.chainid -eq $ChainID} | select createdtime,chainid, fullformattedmessage}
So if something happened last week. Get the Events and tasks in to memory and you can searching for evidence.
I can see how this can be very useful.