VMware Cloud Community
pagne
Contributor
Contributor
Jump to solution

ERROR: New-Snapshot (Operation is not valid due to the current state of the object)

Hello guys!

I've been googlin around a bit, but cannot find a solution to my problem. Hopefully one of you skilled people can point me in the right direction:)

I've created a powerCLI script to take a snapshot of our most important servers before we do our weekly maintenance. The idea to run the script as a regular powershell script and then have it

  1. Add powercli snapins and connect to virtualcenter servers
  2. query the VC servers for VMs in a perticular folder
  3. Run a foreach loop on these servers to create a snapshot with name $snapshotname

Currently i'm just running it on a test folder that I created in vSphere that contains 3 test VMs:

  • Testmagne1 - Working normally
  • Testmagne2 - I've renamed the folder for this VM in the datastore in order to generate an error.
  • testmagne3 - Working normally

Since the servers (not the test servers, but the proper ones) are the most important servers in our environment I want to be able to catch exceptions and errors when i run this script. I do this with the Try - Catch - Finally functions.

The weird thing is this:

The output from Powershell/PowerCLI is different from the output in VirtualCenter(!)

In virtualcenter the output is as follows

  • Testmagne1 - Snapshot is created successfully - As expected
  • Testmagne2 - Snapshot creation fails as it cannot find the .vmx files - As expected
  • Testmagne3 - Snapshot is created successfully - As expected

However, the output from powercli is slightly different:

  • Testmagne1 - Snapshot is created successfully - As expected
  • Testmagne2 - Snapshot creating fails as it cannot find the .vmx files - As expected
  • Testmagne3 - Snapshot fails with the error message: Operation is not valid due to the current state of the object - Huh?


Apparently once the first error is generated all succeeding VMs get the error "Operation is not valid due to the current state of the object"


I've been pulling my hair out trying to figure this problem out, but I am not getting anywhere


Here's some additional information:

1. The Script (short version of it. It generates the same error):

--------------- START SCRIPT-----------------

$ErrorActionPreference = "Stop"

Add-PSSnapin VMware.VimAutomation.Vds, VMware.VimAutomation.Core | Out-Null

Connect-VIServer VIRTUALCENTER1, VIRTUALCENTER2 | Out-Null  # Sensored the VirtualCenter names

$servers = Get-VM -location "testfolder"

$snapshotname = "Testsnapshot"

$verifycreatesnapshot = Read-Host "You are about to create snapshots for $servers. Do you want to continue? y/n"

    if ($verifycreatesnapshot -eq "y"){

        Write-Host "Creating snapshots..."

       

        ForEach ($i in $servers) {

            Write-Host "Creating snapshot for $i"        

            Try {New-Snapshot -VM $i -Name $snapshotname | Out-Null}

            Catch{$_.exception | select * ; Write-Host "Failed to create snapshot for $i";} 

        }

        Write-Host "Command finished"

    }

    else{Write-Host "Operation cancelled by user"}

Read-Host "End of script. Press ENTER to close"

------------------ END SCRIPT -------------------

2. The error messages from PowerCli:

Error for Testmagne 2 (as expected):

File                    : [DATASTORE114] testmagne2/testmagne2.vmx

DynamicType             :

DynamicPropertyInternal :

FaultCause              :

FaultMessage            :

ErrorId                 : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_O

                          perationFailed

ErrorCategory           : NotSpecified

TargetObject            :

RecommendedAction       :

SessionId               :

ConnectionId            : /VIServer=SENSORED:443/

Severity                : Error

Message                 : 31/10/2013 10:52:16    New-Snapshot        The operat

                          ion for the entity "testmagne2" failed with the follo

                          wing message: "File [DATASTORE114] testmagne2/testmagn

                          e2.vmx was not found"

Data                    : {ParameterValues}

InnerException          : VMware.Vim.VimException: The operation for the entity

                           "testmagne2" failed with the following message: "Fil

                          e [DATASTORE114] testmagne2/testmagne2.vmx was not fou

                          nd"

TargetSite              :

StackTrace              :

HelpLink                :

Source                  :

Failed to create snapshot for testmagne2

Error for testmagne3:

Creating snapshot for testmagne3

ErrorId           : Core_BaseCmdlet_UnknownError

ErrorCategory     : NotSpecified

TargetObject      :

RecommendedAction : Error occured while executing cmdlet: New-Snapshot. Check i

                    nner exception for more details.

SessionId         :

ConnectionId      :

Severity          : Error

Message           : 31/10/2013 10:52:17    New-Snapshot        Operation is not

                     valid due to the current state of the object.

Data              : {ParameterValues}

InnerException    : System.InvalidOperationException: Operation is not valid du

                    e to the current state of the object.

                       at VMware.VimAutomation.ViCore.Impl.V1.Task.ViCoreTaskCo

                    reServiceProviderImpl.BeginTaskCompletionPoll(List`1 taskLi

                    st)

                       at VMware.VimAutomation.Sdk.Impl.V1.Task.CoreTaskService

                    Impl.WaitForTask(IEnumerable`1 taskList, ProgressCallback p

                    rogressCallback)

                       at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdl

                    et.EndProcessingErrorHandled()

                       at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseC

                    mdlet.EndProcessingErrorHandled()

TargetSite        : Void ThrowTerminatingError(System.Management.Automation.Err

                    orRecord)

StackTrace        :    at System.Management.Automation.MshCommandRuntime.ThrowT

                    erminatingError(ErrorRecord errorRecord)

HelpLink          :

Source            : System.Management.Automation

Failed to create snapshot for testmagne3

3. PowerCLI version

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.5 Release 1 build 1295336

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI Component 5.5 build 1262826

   VMWare ImageBuilder PowerCLI Component 5.5 build 1262826

   VMware License PowerCLI Component 5.5 build 1265954

   VMware VDS PowerCLI Component 5.5 build 1295334

   VMware vSphere PowerCLI Component 5.5 build 1295334


4. VirtualCenter version

VMware vCenter Server Version 5.0.0 Build 913577

vSphere Client Version 5.0.0 Build 913577

Hosts: VMware ESXi 5.0.0 Build 914586

If you need any additional information, just let me know

Any help is greatly appreciated

Thanks!

- Pagne



21 Replies
Dean716
Contributor
Contributor
Jump to solution

Thank you RonPSSC. After tearing my hair out over this for a few hours, I found your post, restarted my PowerShell session, and all is good.

0 Kudos
virtualdan1
Contributor
Contributor
Jump to solution

Add me into the tearing my hair out group who stumbled across your solution. Didn't even think to terminate and re-open the PowerCLI session...but it worked!

0 Kudos