VMware Cloud Community
CRad14
Hot Shot
Hot Shot
Jump to solution

Getting Previous vCenter Tasks

So I am just having an issue finding where the previous tasks are stored

Get-task only does tasks in the recent pane, and get-vievent only seems to cover the Events portion of the Tasks/Events tab in vCenter.

I just can't find where that big list of Past tasks is located...

Any help would be aprpeciated...

Thanks!

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

They are in the events, and they are of type TaskEvent.

You can extract them as follows

Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) |
where {$_.GetType().Name -eq "TaskEvent"}

Anything particular you were looking for ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

They are in the events, and they are of type TaskEvent.

You can extract them as follows

Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples ([int]::MaxValue) |
where {$_.GetType().Name -eq "TaskEvent"}

Anything particular you were looking for ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

Nope that is perfect, I am working on a script and for part of it I wanted to be able to find specific tasks from the recent tasks pane later on in the big Tasks list...

It looks like the matching value is the get-task property id    and the get-vievent property info.task


Thanks LucD! I also think its awesome that in your helpful posts you always link to the SDK Smiley Happy  . Great Stuff as Always

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos
aerodevil
Hot Shot
Hot Shot
Jump to solution

Most Excellent!  I was just thinking about this the other day!  Thanks guys!

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

Ok, one more quick question.

Get-VIEvent |?{($_.gettype().name -eq "taskevent")}

I am trying to find where in that object it actually states whether the task errored or completed.

I know I can filter with get-vievent -type, however I don't know its status and I want the get-vievent object to tell me....I see the Error, Result, Progress properties under Info, but I am looking at an event that did fail and all of those properties are empty. So I am not sure how to script a return of the tasks result.

I know I can get the Result and Error from a get-task, but I am trying to get it from get-vievent...

Any other assistance would be much appreciated!!!

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To get the actual completion status of the tasks we will need to use the TaskHistoryCollector.

Something like this

$hours = 24 # Number of hours back 
$start
= (Get-Date).AddHours(-$hours) $tasknumber = 999 # Windowsize for task collector
$taskMgr = Get-View TaskManager $tFilter = New-Object VMware.Vim.TaskFilterSpec $tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime $tFilter.Time.beginTime = $start
$tFilter.Time.timeType = "startedTime"

$tCollector
= Get-View ($taskMgr.CreateCollectorForTasks($tFilter)) $dummy = $tCollector.RewindCollector $tasks = $tCollector.ReadNextTasks($tasknumber) while($tasks){     foreach($task in $tasks){         New-Object PSObject -Property @{             Name = $task.EntityName             Task = $task.DescriptionId             Start = $task.StartTime             Finish = $task.CompleteTime             Result = $task.State             User = $task.Reason.UserName         }     } } $tasks = $tCollector.ReadNextTasks($tasknumber) # By default 32 task collectors are allowed. Destroy this task collector.
$tCollector
.DestroyCollector()

You can find another example, that also looks at the associated event chain, in my Events – Part 8 – vMotion history post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vcubegiovannive
Contributor
Contributor
Jump to solution

Hello everybody,

how to query for past task from Id?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Assuming you have the Task object in variable $task, you could do

Get-VIEvent -MaxSamples ([int]::MaxValue) |

where {$_ -is [VMware.Vim.TaskEvent] -and $task.Id.Replace('Task-','') -eq $_.Info.Key}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vcubegiovannive
Contributor
Contributor
Jump to solution

Thanks LucD‌ for your reply.

I noticed that $tasknumber could not be grater than 1000, isn't it?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which property are you referring to in $tasknumber ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vcubegiovannive
Contributor
Contributor
Jump to solution

$tasknumber = 999 # Windowsize for task collector

I think it's a simple variable

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is the windowsize for the TaskHistoryCollector used in the other script.

Each time you do a ReadNextTasks, you get that many, or less, task objects returned.

It is not related to the Task itself.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vcubegiovannive
Contributor
Contributor
Jump to solution

Is there a way to get all tasks from a past point in time?

windowsize variable limits this behaviour...

Reply
0 Kudos
baburaju
Contributor
Contributor
Jump to solution

Hi Lucd,

Is any parameter is there to get the previous vCenter tasks on hourly basis I mean suppose I want to see the taks for last 2 -3 hours how can I do that.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will have to use the Start and Finish parameters on the Get-VIEvent cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sasideep
Contributor
Contributor
Jump to solution

Hi,

with Get-Task I am getting the details of currently queued/running etc. However in vSphere client under Recent Tasks, I am noticing some tasks which are 15 days and older. is there a cmdlet I can use to get the older tasks also in Recent Tasks tab.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, not in the Web Client (if that is what you mean).
Use the Get-VIEvent cmdlet and filter on TaskEven objects.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos