PowerCLI

 View Only
Expand all | Collapse all

Getting Previous vCenter Tasks

vcubegiovannive

vcubegiovanniveApr 14, 2016 07:33 AM

  • 1.  Getting Previous vCenter Tasks

    Posted Feb 07, 2012 06:39 PM

    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!



  • 2.  RE: Getting Previous vCenter Tasks
    Best Answer

    Posted Feb 07, 2012 06:50 PM

    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 ?



  • 3.  RE: Getting Previous vCenter Tasks

    Posted Feb 07, 2012 07:04 PM

    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 :smileyhappy:  . Great Stuff as Always



  • 4.  RE: Getting Previous vCenter Tasks

    Posted Feb 07, 2012 07:59 PM

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



  • 5.  RE: Getting Previous vCenter Tasks

    Posted Feb 11, 2012 06:22 PM

    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!!!



  • 6.  RE: Getting Previous vCenter Tasks

    Posted Feb 12, 2012 01:18 AM

    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.



  • 7.  RE: Getting Previous vCenter Tasks

    Posted Jan 22, 2019 04:11 AM

    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.



  • 8.  RE: Getting Previous vCenter Tasks

    Posted Jan 22, 2019 05:47 AM

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



  • 9.  RE: Getting Previous vCenter Tasks

    Posted Aug 20, 2019 07:55 AM

    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.



  • 10.  RE: Getting Previous vCenter Tasks

    Posted Aug 20, 2019 08:00 AM

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



  • 11.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 07:33 AM

    Hello everybody,

    how to query for past task from Id?



  • 12.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 07:48 AM

    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}



  • 13.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 09:16 AM

    Thanks LucD‌ for your reply.

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



  • 14.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 09:24 AM

    Which property are you referring to in $tasknumber ?



  • 15.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 09:30 AM

    $tasknumber = 999 # Windowsize for task collector

    I think it's a simple variable



  • 16.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 09:38 AM

    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.



  • 17.  RE: Getting Previous vCenter Tasks

    Posted Apr 14, 2016 01:02 PM

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

    windowsize variable limits this behaviour...



  • 18.  RE: Getting Previous vCenter Tasks

    Posted Jun 20, 2025 08:30 AM

    Hello LucD,

    How Can I use similarly on the individual esxi host to get the Tasks? please help