VMware Cloud Community
KWKirchner
Enthusiast
Enthusiast

PowerCLI issue with snapshot removal - Operation is not valid due to the current state of the object

I have a script I run to delete snapshots that are older than 15 days.  This works fine, except that the actual call to the remove-snapshot cmdlet returns this error:

Remove-Snapshot : 9/18/2015 4:17:51 AM    Remove-Snapshot        Operation is not valid due to the current state of the object.

At T:\VMware\PowerCLIScripts\Clean_Snapshots_Sync.ps1:17 char:11

+ $vm | Remove-Snapshot -Confirm:$false

+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   

+ CategoryInfo          : NotSpecified: (:) [Remove-Snapshot], VimException

+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveSnapshot

But it still works... it just puts it in the vCenter queue.  That's fine except that this has the effect of making this removal run asynchronously even without the -RunAsync flag being set. So I cant just itterate through a list of snapshots. I have to write a loop that monitors the number of snapshot removal tasks that are currently running and sleep until there only 4 (my arbitrary number) running.

So it works, but I dont know why I am getting that error about the object state.  The queued snapshot removal job begins almost immediately, so what state could it be in for a second that makes the removal report a failure? If it fails, why does it still queue the job? The list of snapshots are pulled into an array prior to looping through the array for the removal, so it shouldnt be interfering with the removal task.

$days_old=15
$max_tasks=4

$VM_Snapshot_List = Get-VM | Get-Snapshot | where { $_.Created -lt (Get-Date).AddDays(($days_old*-1))}

foreach ($snapshot in $VM_Snapshot_List){
    $snapshot | Select VM,Name,Created

    $running_tasks = get-task -Status Running | where { $_.Name -eq "RemoveSnapshot_Task"}

    while ( $running_tasks.count -ge $max_tasks ){
         write-host "Too many remove tasks! Sleeping for 30 seconds..."
         sleep 30
         $running_tasks = get-task -Status Running | where { $_.Name -eq "RemoveSnapshot_Task"}
    }
    $snapshot | Remove-Snapshot -Confirm:$false
}
4 Replies
vbrad6841
Enthusiast
Enthusiast

Are you running the code in the bottom of your post and that is the code that is generating the error?

What version of ESX and PowerCLI?

Blog: https://lowercasevblog.wordpress.com/
0 Kudos
bansne
Enthusiast
Enthusiast

You should be able to get the cause of issue , check hostd logs it will tell you what is the warning all about .

As in my case it was Invalid transition requested (VM_STATE_ON_SHUTTING_DOWN -> VM_STATE_CREATE_SNAPSHOT): Invalid state error , all i did was restarted management agent on host and vCenter Services executed task again to fix.

KWKirchner
Enthusiast
Enthusiast

Yes, the code is on the bottom. This is ESXi v5.5 and PowerCLI 5.8 (Build 2057893)

0 Kudos
Chirag235
Enthusiast
Enthusiast

I'm getting same error while i removing snapshots using powershell. 

When i run this command

Get-Snapshot -VM "testmachine" | Remove-Snapshot -Confirm:$false 

at that time getting such error 

  • Remove-Snapshot : 2/1/2024 5:28:35 PM Remove-Snapshot Operation is not valid due to the current state of the object.
    At line:1 char:58
    + ... testmachine" | Remove-Snapshot -Confirm:$false
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Remove-Snapshot], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveSnapshot

but when i checked in in vcenter task the snapshot removing process is running, also i checked using get-task command.

Have you resolved this issue?

0 Kudos