How to check and generate report of Backup ,Cloning ,Snapshot or any Other Task of VM/ESXI/datastore/Anything in vSphere using PowerCli

Often there are issues in environment of Cloning, Backup or snapshot,where People want to check how many task has failed or success . To check this we usually use Task Tab of VM/ESXI or any Object in vSphere.

To check the failure we usually try to use filter and do the next page and next page and next page…

This can be very time consuming and annoying some time. To avoid the click of Next page .I have come up with this function which can help to view and filter 1000 Task directly using PowerCli.

function Get-VIObjectTask
{
<#
.NOTES
===========================================================================
Created by: Ankush Sethi
Blog:       www.vmwarecode.com
===========================================================================
.SYNOPSIS
Check the Task of any Object in vSphere
.DESCRIPTION
Function will provide the task history of any object like vm,esxi,datastore,distributed switch etc.
.PARAMETER Entity
Enter the Entity name for which you want to check the task details.
.PARAMETER Recursion
Use this parameter if you want to check the child object also like vm is child object of esxi.
.PARAMETER TaskName
Use this parameter to filter the task just by specifying the ketword
.Parameter Initiator
Use this parameter to filter the task just by giving the Username
.Parameter ExportToCsv
Use this parameter to generate the csv report of task.
.EXAMPLE
example 1>Get-vmhost esxiname|get-viobjecttask
example 2>Get-vmhost esxiname|get-viobjecttask -recursion:$true
example 3>Get-vmhost esxiname|get-viobjecttask -recursion:$true -taskname "clone"
example 4>Get-vmhost esxiname|get-viobjecttask -recursion:$true -Initiator "admin"
example 5>Get-vmhost esxiname|get-viobjecttask -recursion:$true -TaskName "clone" -Initiator "admin"
example 6>Get-vmhost esxiname|get-viobjecttask -recursion:$true -TaskName "clone" -Initiator "admin" -ExportToCSV:$true
#>

param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl]$Entity,
[Switch]$Recursion,
[int]$MaxSample=[int]1000,
[ValidateNotNullOrEmpty()][string]$TaskName,
[ValidateNotNullOrEmpty()][string]$Initiator,
[switch]$ExportToCsv


)

Process{
try{
$Inv=Get-Inventory -Name $Entity -ErrorAction Stop
}
catch
{
Write-Error -Message "Please enter the correct name of Object" -ErrorAction Stop
}
$TaskObject=New-Object VMware.Vim.TaskFilterSpec
$Entityobject=New-Object VMware.Vim.TaskFilterSpecByEntity
$TaskObject.Entity=$Entityobject
$TaskObject.Entity.Entity=$Entity.ExtensionData.moref
If($Recursion -eq $true){$TaskObject.Entity.Recursion=[VMware.Vim.TaskFilterSpecRecursionOption]::all}
$TskMgr=Get-View TaskManager
$taskcollected= $TskMgr.CreateCollectorForTasks($TaskObject)
$TaskView=Get-View $taskcollected
$taskout=$TaskView.ReadNextTasks($MaxSample)
If(($TaskName -ne $null) -and ($Initiator -eq $null))
{
$output=$taskout|?{$_.name -match $TaskName}
}
elseif($TaskName -eq $null -and $Initiator -ne $null)
{
$output=$taskout|?{$_.reason.username}
}
elseif(($TaskName -eq $null) -and ($Initiator -eq $null))
{
$output=$taskout
}
elseif(($TaskName -ne $null) -and ($Initiator -ne $null))
{
$output=$taskout|?{$_.name -match $TaskName -and $_.reason.username -match $Initiator}

}

}
End{
$Report=$output|select Name,DescriptionId,EntityName,
@{N="QueueTime";E={[string]($_.StartTime-$_.QueueTime).Milliseconds+"ms"}},
StartTime,CompleteTime,
@{N="ExecutionTime";E={[string]($_.CompleteTime-$_.StartTime).Milliseconds+"ms"}},
@{N="Initiator";E={$_.reason.username}},
@{N="Status";E={If($_.state -match "error"){[string]$_.Error.LocalizedMessage}else{$_.state}}}
$Report|ft -AutoSize
If($ExportToCsv)
{
$path=Get-Location
$name="\VMwareCode_TaskReport"+" "+($global:DefaultVIServer).name.Split('.')[0]+".csv"
$Reportname= $path.path+$name
$Report|Export-Csv -NoTypeInformation -Path $Reportname
}

$TaskView.DestroyCollector()
}
}





Prerequisite and Limitation:

We should be connected to only 1 vCenter using below command.

Connect-VIServer -Server vCenterName -User UserName -Password Password

This will provide you only last 1000 results of one object.
You should have permission to create the csv file .

How to use the Function

  1. Copy the code from above and paste it into Notepad.
  2. Save it with anyname with extension of .ps1
  3. Open the PowerCli and to go the directory where it is saved using cd command
  4. Run the command . .\FileName.ps1 (Note :There is a space between 2 dots.)
  5. Function is loaded in local session of PowerCli and you are good to use this as cmdlet.

Once you have completed above 5 steps , we are good to use this function as cmdlet of Powercli.

Check the below sample usage now.

There are total 6 ways to use this cmdlet. Note you use this against any object(vm/esxi/datatsore etc). I am taking example of ESXi to demonstrate here.

Sample usage with just ESXi name

Get-VMHost ESXiName|Get-VIObjectTask

Sample usage with Recursion parameter: Use this parameter to check the task of child object also like VM is child object ESXi.

Get-VMHost ESXiName|Get-VIObjectTask -Recursion:$true

Sample Usage with TaskName parameter: Use this parameter to filter the task by just providing the task keyword. No need to rember or type full taskname.

Get-VMHost ESXiName|Get-VIObjectTask -Recursion:$true -TaskName "clone"

Sample usage with Initiator parameter: Use this parameter to filter the task by just providing the username.It can be anything like service account name or domain username.

Get-VMHost ESXiName|Get-VIObjectTask -Recursion:$true -Initiator "admin"

Sample usage with bothTaskName and Initiator parameter:Function can be used with combining both filters and provide you the result.

Get-VMHost ESXiName|Get-VIObjectTask -Recursion:$true -Initiator "admin" -TaskName "clone"

Sample Usage with ExportToCsv argument: Use this argument to generate the result to csv file.

Get-VMHost ESXiName|Get-VIObjectTask -Recursion:$true -Initiator "admin" -TaskName "clone" -ExportToCsv:$true

Note: If you are unable to view the status property you can maximize the PowerCLI/PowerShell window with below.

Right Click on top bar and use the below screenshot to adjust the window size.

,

One response to “How to check and generate report of Backup ,Cloning ,Snapshot or any Other Task of VM/ESXI/datastore/Anything in vSphere using PowerCli”

  1. Thanks for the script, this was very useful to share the snapshot task to the backup team, they had raised a INC to our team, claiming snapshot is failing, where as snapshot was getting created and the issue was from the backup solution. It would have been difficult to for us to manually create the snapshot and generate the task, especially when there is a lot of VMs to look at.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: