You can also change the Task filter.
This should be a bit faster and place less stress on your vCenter, since only Task objects fitting the filter will be returned.
Lines 10-15 need to be changed to something like this
$tFilter=New-Object VMware.Vim.TaskFilterSpec
$tFilter.Time=New-Object VMware.Vim.TaskFilterSpecByTime
$tFilter.Time.beginTime=(Get-Date).AddHours(-$hours)
$tFilter.Time.timeType="startedTime"
$tFilter.Entity=New-Object VMware.Vim.TaskFilterSpecByEntity
$tFilter.Entity.Entity=(Get-VM-Name <your-VM>).ExtensionData.MoRef
$tCollector=Get-View($taskMgr.CreateCollectorForTasks($tFilter))