VMware Cloud Community
atrockz
Contributor
Contributor
Jump to solution

List all the task on VCenter Server

Hi all,

I am new to VMware PowerCLI and still got lots to learn. I am trying to get the list of all the task performed(running and completed) on VC Server but i am not sure how to do that. I tried using Get-Task Cmdlet but it gives me only the current or recent tasks. While browsing throught VI Client I can see lots of task details. Is there any direct Cmdlet to get the list of all the task or do i need to write some PowerShell script ?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is unfortunately no Get-VITask cmdlet.

The solution is to use the SDK TaskCollector.

Something like this

$hours = 6 # Number of hours back
$tasknumber = 999 # Windowsize for task collector
$eventnumber = 100 # Windowsize for event collector

$report = @()

$taskMgr = Get-View TaskManager

$tFilter = New-Object VMware.Vim.TaskFilterSpec
$tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$tFilter.Time.beginTime = (Get-Date).AddHours(-$hours)
$tFilter.Time.timeType = "startedTime"

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

$dummy = $tCollector.RewindCollector
$tasks = $tCollector.ReadNextTasks($tasknumber) | Sort-Object -Property StartTime

$tasks | % {
	$report += New-Object PSObject -Property @{
		EntityName = $_.EntityName
		Start = $_.StartTime
		Finish = $_.CompleteTime
		Result = $_.State
		User = $_.Reason.UserName
		Description = $_.DescriptionId
	}
}

# By default 32 task collectors are allowed. Destroy this task collector.
$tCollector.DestroyCollector()

$report

But to get the complete information like you see in the vSphere client, you would also need to collect the corresponding events for the task.

See my Events – Part 3 : Auditing VM device changes post for a sample.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Get-VIEvent cmdlet for this.

If you filter on the tasks you limit what is returned.

Also watch out with the time range you specify, it could return thousands of events and the cmdlet will take a long time before it returns the data.

This should give you some information from the last 4 hours

$events = Get-VIEvent -Start (Get-Date).addhours(-4)
$events | %{
	Write-Host $_.GetType().Name $_.UserName $_.CreatedTime $_.FullFormattedMessage
}

I'm not sure what you are after so this is a rather general example.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
atrockz
Contributor
Contributor
Jump to solution

In VI Client connected to VC Server, under "Tasks & Events" tab, we can view either the tasks or events window. Events windows shows all the different types of events like Error, Info, and Warning on VC Server. Tasks windows shows all the tasks like power on VM, power off VM, clone VM, Delete VM etc. I am trying to list all the entries under task window and not events.

Get-VIEvents gives me only the events.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is unfortunately no Get-VITask cmdlet.

The solution is to use the SDK TaskCollector.

Something like this

$hours = 6 # Number of hours back
$tasknumber = 999 # Windowsize for task collector
$eventnumber = 100 # Windowsize for event collector

$report = @()

$taskMgr = Get-View TaskManager

$tFilter = New-Object VMware.Vim.TaskFilterSpec
$tFilter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$tFilter.Time.beginTime = (Get-Date).AddHours(-$hours)
$tFilter.Time.timeType = "startedTime"

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

$dummy = $tCollector.RewindCollector
$tasks = $tCollector.ReadNextTasks($tasknumber) | Sort-Object -Property StartTime

$tasks | % {
	$report += New-Object PSObject -Property @{
		EntityName = $_.EntityName
		Start = $_.StartTime
		Finish = $_.CompleteTime
		Result = $_.State
		User = $_.Reason.UserName
		Description = $_.DescriptionId
	}
}

# By default 32 task collectors are allowed. Destroy this task collector.
$tCollector.DestroyCollector()

$report

But to get the complete information like you see in the vSphere client, you would also need to collect the corresponding events for the task.

See my Events – Part 3 : Auditing VM device changes post for a sample.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
atrockz
Contributor
Contributor
Jump to solution

Hi LucD,

It works great. All i wanted was task count and small description and its all there.

Thanks alots Smiley Happy

0 Kudos