Hi,
I have the following PowerCLI script that I've hacked together from numerous examples. The problem is it works for a while then starts repeating the following error before for every line.
error:
Exception calling "CreateCollectorForTasks" with "1" argument(s): "The operation is not allowed in the current state."
At C:\myscript.ps1:37 char:72
+ $taskCollector = Get-View ($taskMgr.CreateCollectorForTasks <<<< ($filter))
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
script (myscript.ps1):
$VISRV = "xxxxxxxxxxxx"
Connect-VIServer $VISRV
Write-Host "VM Name,Snapshot Name,Snapshot Size,Date Created,Creator"
$myVMs = Get-VM
$VMsWithSnaps = @()
foreach ($vm in $myVMs) {
$snapshots = Get-Snapshot -VM $vm
foreach ($snapShot in $snapShots){
$VM = $snapShot.VM
$Name = $snapShot.Name
$Created = $snapShot.Created
$Description = $snapShot.Description
$Size = $snapShot.SizeMB
#ignore VEEAM restore points
if ((!($VM)) -or ($Name -match "Restore Point")){
continue
}
if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
#getting the snapshot creator is a bit more complex.
$taskMgr = Get-View TaskManager
#define the number of tasks to return
$numberofTasks = 999
#create a filter based on the snapshot creation time (5 seconds before $created)
$filter = New-Object VMware.Vim.TaskFilterSpec
$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$filter.Time.beginTime = (($Created).AddSeconds(-5))
$filter.Time.timeType = "startedTime"
#get all the tasks based on the filter
$taskCollector = Get-View ($taskMgr.CreateCollectorForTasks($filter))
#rewind to the begin time specified in the filter (setting equal to blah just keeps it's output off the screen)
if ($taskCollector -ne $null)
{
$blah = $taskCollector.RewindCollector
#read in the first 999 tasks from our starting point (which is 5 seconds before $created)
$allTasks= $taskCollector.ReadNextTasks($numberofTasks)
#crank through those first 999 tasks
if ($allTasks -ne $null)
{
foreach ($task in $alltasks){
#find the createSnapshot task for $VM
if ($task -ne $null)
{
if (($task.DescriptionId -eq "VirtualMachine.createSnapshot") -and ($task.EntityName -eq $VM)){
#Get the reason username which is our creator
$creator = $task.reason.username
}
}
}
}
}
Write-Host $VM","$Name","$Size","$Created","$creator }
}
}
$VMsWithSnaps | Sort CreationDate
Regards,