VMware Cloud Community
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

How to get the Virtual Machine Tasks completed time using PowerCLI

Friends,

I am trying to get the task completed time for a particular task type using Powercli. Any help appreciated.  I am getting StartTime of the Tasks using Get-VIEvent . How to get the Task completion time for a particular VM.

I am looking for a Backup Job status for all VM's in the vCenter, whether the backup job is successful / failed from the last 24 hrs.

I am able to get the particular task and start time using below command

Get-VM <VM Name> | Get-VIEvent | Where {$_.username -eq "username" -and $_.Info.DescriptionId -eq "task type" -and $_.CreatedTime -gt (Get-Date).AddHours(-24) }

Help is required to get the required task type completion time and completion status ( successful / failed / error )

Thanks,

Naga Suresh.

0 Kudos
1 Solution

Accepted Solutions
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

I love this.  I came up with the below script which will suffice my need.  Thanks for your support.

$hours = 24 # Number of hours back

$start = (Get-Date).AddHours(-$hours)

$tasknumber = 100 # Windowsize for task collector

$taskMgr = Get-View TaskManager

$tFilter = New-Object VMware.Vim.TaskFilterSpec

$tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime

$tFilter.Entity = New-Object VMware.Vim.TaskFilterSpecByEntity

$tFilter.Time.beginTime = $start

$tFilter.Time.timeType = "startedTime"

foreach($vms in Get-VM | Sort-Object Name){

foreach($vm in $vms){

$vmMoRef = (Get-VM $vm | Get-View).MoRef

$tFilter.entity.entity = $vmMoRef

$tCollector = Get-View ($taskMgr.CreateCollectorForTasks($tFilter))

$dummy = $tCollector.RewindCollector

$tasks = $tCollector.ReadNextTasks($tasknumber)

foreach($task in $tasks){

if($task.DescriptionId -eq "com.emc.networker.backup.task" -and $task.State -ne "success" ){

        Write-Host $task.EntityName $task.DescriptionId $task.StartTime $task.CompleteTime $task.State $task.Reason.UserName

  }

    }

$tCollector.DestroyCollector()

}

}

I know this will take more time, if you have lot of VM's in a vCenter. But it will saperate all tasks for each VM, which suffice my need.

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Can you provide some details on the task?

How is it started? A scheduled task? A call to cmdlet with the RunAsync option? A call from an external program to an API method?

Did you check the events related to the Task?


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

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

The task is a scheduled backup tasks from EMC Networker Backup.

I can find the Event for the task.  I can get Start Time from the command.

Question : How to get the task Completed Time, Successful or failed  using PowerCLI.

Note : I need to check the backup event task for each every VM and get whether the last run of the backup job is successful, failed or still running .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I meant how does this Task look in the Web Client?

Take for example this random task from my test environment (Exit Maintenance mode).

The Task has a number related events, in which you should normally find the end time.

task.jpg

By following the chained events, you can get to the Finish time of the Task.

For example

$start = (Get-Date ).AddDays(-1)

$tasks = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.TaskEvent] -and $_.FullFormattedMessage -match 'Task: Exit maintenance mode'}


foreach($task in $tasks){

   $events = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_.ChainId -eq $task.Chainid} |

   Sort-Object -Property Key

   $task | Select @{N='Name';E={$task.Info.Name}},

   @{N='Start';E={$events[0].CreatedTime}},

   @{N='Finish';E={$events[-1].CreatedTime}}

}

To get at the status of the Task after completion, I would need some more information on the Task itself.


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

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

Negative.

if you see the tasks for a particular VM. I need both Start Time and Completed Time.  I need to capture this using PowerCLI.

The related events are showing only one entry. With your script, i am getting both Start and End time as same.

pastedImage_0.png

The above screenshot shows under VM Tasks section.

Question is how to capture the VM Particular Task  with Start and Completed Time.

Your script output .

Name Start                 Finish

---- -----                 ------

     10/19/2018 6:10:31 AM 10/19/2018 6:10:31 AM

     10/19/2018 6:10:17 AM 10/19/2018 6:10:17 AM

     10/19/2018 6:10:14 AM 10/19/2018 6:10:14 AM

     10/19/2018 6:09:55 AM 10/19/2018 6:09:55 AM

     10/19/2018 6:00:41 AM 10/19/2018 6:00:41 AM

     10/19/2018 6:00:40 AM 10/19/2018 6:00:40 AM

     10/19/2018 6:00:39 AM 10/19/2018 6:00:39 AM

     10/19/2018 6:00:38 AM 10/19/2018 6:00:38 AM

     10/19/2018 6:00:37 AM 10/19/2018 6:00:37 AM

     10/19/2018 6:00:37 AM 10/19/2018 6:00:37 AM

     10/19/2018 6:00:34 AM 10/19/2018 6:00:34 AM

   

I need a way to pass VM Name as a parameter and pull the required tasks Start Time , Completed Time and Status using PowerCLI.

Thanks,

Naga Suresh

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

using Get-VIEvent , i am not getting Completed Time.

I can see all the required data in VPX_TASK table in vCenter DB.  Question is how to extract that data from PowerCLI.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Note that Get-VIEvent returns the CreatedTime in UTC, you have to convert to local time to see the same times.

What did you use in the Where-clause?

Can you include the script that you are using?


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

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

I used the same script which you provided with little change in where clause

$start = (Get-Date ).AddDays(-1)

$tasks = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'com.emc.networker.backup.task'}

foreach($task in $tasks){

   $events = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where{$_.ChainId -eq $task.Chainid} |

   Sort-Object -Property Key

   $task | Select @{N='Name';E={$task.Info.EntityName}},

   @{N='Start';E={$events[0].CreatedTime}},

   @{N='Finish';E={$events[-1].CreatedTime}}

}

The output of the script , i placed in the above reply.

I even tried to pull all events initiated by a particular user , but i am not getting 2 entries to pull Start and End times for a chain id

Get-vm <VM> | Get-VIEvent | Where {$_.username -eq "<User>" -and $_.CreatedTime -gt (Get-Date).AddHours(-24)}

I will dig more on the UTC time part and will update my findings.

thanks for your support on this.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since this is a Task from an external application or extension, I'm not sure there are related Events to the actual Task event.

You could check in the Web CLient, select the Task and then verify if there Related Events shown in the bottom part of the screen (see my earlier screenshot)


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

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

I am seeing only one related event for the tasks which i am looking for.

Need to dig the mystery of completion time where it is saving and showing in Task Pane of the VM.

0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

I love this.  I came up with the below script which will suffice my need.  Thanks for your support.

$hours = 24 # Number of hours back

$start = (Get-Date).AddHours(-$hours)

$tasknumber = 100 # Windowsize for task collector

$taskMgr = Get-View TaskManager

$tFilter = New-Object VMware.Vim.TaskFilterSpec

$tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime

$tFilter.Entity = New-Object VMware.Vim.TaskFilterSpecByEntity

$tFilter.Time.beginTime = $start

$tFilter.Time.timeType = "startedTime"

foreach($vms in Get-VM | Sort-Object Name){

foreach($vm in $vms){

$vmMoRef = (Get-VM $vm | Get-View).MoRef

$tFilter.entity.entity = $vmMoRef

$tCollector = Get-View ($taskMgr.CreateCollectorForTasks($tFilter))

$dummy = $tCollector.RewindCollector

$tasks = $tCollector.ReadNextTasks($tasknumber)

foreach($task in $tasks){

if($task.DescriptionId -eq "com.emc.networker.backup.task" -and $task.State -ne "success" ){

        Write-Host $task.EntityName $task.DescriptionId $task.StartTime $task.CompleteTime $task.State $task.Reason.UserName

  }

    }

$tCollector.DestroyCollector()

}

}

I know this will take more time, if you have lot of VM's in a vCenter. But it will saperate all tasks for each VM, which suffice my need.

0 Kudos