I am trying to write script that will send me the start time and the completed time for snapshots that are created during the VEEAM backup process. I run the following command to pull my tasks:
get-vm <VM NAME> | get-VIEvent -Start "11/15/2022 17:00:00" | Where Info
I then run the following command to look at the data:
$Tasks.Info
This command returns fields for the STARTTIME and COMPELETEDTIME but these fields are empty. Can anyone explain why these fields are empty or where the vCenter web client is pulling this Start and End Time from?
That is because the Get-Task cmdlet unfortunately only looks at the RecentTask property in the TaskManager.
Which limits the output to approximately the last 10 minutes.
But you can use the TaskManager to see all Tasks (for as long as they are retained on your vCenter at least).
Something like this for example
$start = (Get-Date).AddHours(-24)
$taskMgr = Get-View TaskManager
# First do the recent tasks
Get-View -Id $taskMgr.RecentTask |
Where-Object { $_.Info.Name -eq 'CreateSnapshot_Task' } |
ForEach-Object -Process {
New-Object -TypeName PSObject -Property ([ordered]@{
VM = $_.Info.EntityName
State = $_.Info.State
Start = $_.Info.StartTime
Finish = $_.Info.CompleteTime
})
}
# Next are the historic tasks
$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) {
$tasks | Where-Object { $_.Name -eq "CreateSnapshot_Task" } | ForEach-Object {
New-Object -TypeName PSObject -Property ([ordered]@{
VM = $_.EntityName
State = $_.State
Start = $_.StartTime
Finish = $_.CompleteTime
})
}
$tasks = $tCollector.ReadNextTasks($tasknumber)
}
# By default 32 task collectors are allowed. Destroy this task collector.
$tCollector.DestroyCollector()
Note that for a brief moment in time a Snapshot Task might be present in the RecentTask property and in the Historic Task data.
That can cause one or more duplicate entries in the results.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The event you are capturing is created when the Snapshot is started.
You'll notice that it also says under Info.State that the Task is queued, not Completed
When the Snapshot is completed, you can retrieve the Task object, which will then contain the Start- and CompletedTime.
$event = Get-VM -Name <VM NAME> | Get-VIEvent | Where-Object { $_.Info.Name -eq 'CreateSnapshot_Task' }
$task = Get-Task -Id $event.Info.Task
$task | Select State,Description,StartTime,FinishTime
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I appreciate the reply. The problem I am having is that I want to run this script to look for snapshots created in the last twenty four hours. the GET-TASK command will only retrieve recent tasks and not older tasks. So I cannot use that command to retrieve information for tasks that have completed more than ten minutes ago.
That is because the Get-Task cmdlet unfortunately only looks at the RecentTask property in the TaskManager.
Which limits the output to approximately the last 10 minutes.
But you can use the TaskManager to see all Tasks (for as long as they are retained on your vCenter at least).
Something like this for example
$start = (Get-Date).AddHours(-24)
$taskMgr = Get-View TaskManager
# First do the recent tasks
Get-View -Id $taskMgr.RecentTask |
Where-Object { $_.Info.Name -eq 'CreateSnapshot_Task' } |
ForEach-Object -Process {
New-Object -TypeName PSObject -Property ([ordered]@{
VM = $_.Info.EntityName
State = $_.Info.State
Start = $_.Info.StartTime
Finish = $_.Info.CompleteTime
})
}
# Next are the historic tasks
$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) {
$tasks | Where-Object { $_.Name -eq "CreateSnapshot_Task" } | ForEach-Object {
New-Object -TypeName PSObject -Property ([ordered]@{
VM = $_.EntityName
State = $_.State
Start = $_.StartTime
Finish = $_.CompleteTime
})
}
$tasks = $tCollector.ReadNextTasks($tasknumber)
}
# By default 32 task collectors are allowed. Destroy this task collector.
$tCollector.DestroyCollector()
Note that for a brief moment in time a Snapshot Task might be present in the RecentTask property and in the Historic Task data.
That can cause one or more duplicate entries in the results.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference