VMware Cloud Community
vmpwcliuser31
Contributor
Contributor

Exporting a VM's Tasks

Hello all,

I'm trying to export the Tasks for a specific VM.  The columns that I'm really interested in are the Task Name, Start Time, Completion Time, & Execution Time (as seen in the attached screenshot).


Is there a way I can do that via PowerCLI?

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Use the Get-VIEvent cmdlet and filter on TaskEvent, then do a Select-Object on the returned objects.

For example

 

$start = (Get-Date).AddDays(-7)
$vm = Get-VM -Name MyVM

Get-VIEvent -Entity $vm -Start $start -MaxSamples ([int]::MaxValue) |
Where {$_ -is [VMware.Vim.TaskEvent]}

 

 


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

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast

Hi.

I can also advise a good function Get-TaskPlus. Author LucD.

Reply
0 Kudos
vmpwcliuser31
Contributor
Contributor

Thanks for the information but still need some more direction.

I've worked with the Get-VIEvents a little before but all that shows is the Creation (or Start) Time of an event.  How can I dig around to see where the Execution Time column is getting its data from?  Is the object called "Execution Time" or something else?

 

 

Also, I saw your Get-TaskPlus script but can't figure out how to get that to work either.  😕  

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast

LucD blog has a detailed description and examples.
But you will need to add "Execution time". For example, after the line with "...$object.Add("Task Ended", $_.Complete Time)...".

Example of using function Get-TaskPlus

$vm = Get-vm -name 'Vm_Name'
Get-TaskPlus -Entity $vm -MaxSamples 1500 -Details | Export-Csv -Path 'C:\Vm_Tasks.csv' -NoTypeInformation -UseCulture

Example of a string for "execution time"

if ($Details) { $RunTime = New-TimeSpan -Start $_.StartTime.ToLocalTime() -End $_.CompleteTime.ToLocalTime()
$object.Add("Execution time", $("{0}d {1}h {2}m, {3}s." -f $RunTime.Days, $RunTime.Hours, $RunTime.Minutes, $RunTime.Seconds)) }
Reply
0 Kudos
vmpwcliuser31
Contributor
Contributor

Okay, after working with my co-worker, we figured out what I was missing.  x_x  (I had LucD's Get-TaskPlus script in a file and was trying to call the script/file itself instead of having the work underneath the function/script itself).

 

I got it to work and appreciate the help!  😄

Reply
0 Kudos