VMware Cloud Community
MariusRoma
Expert
Expert

Getting a return code from new-snapshot

In a previous post (see Return code from new-snapshot ) I asked how to get a return code from new-snapshot.

I created a script line like:

$my_snapshot = new-snapshot -VM $my_vm -name $snapshot_neme -Description $description

        if ($my_snapshot)

            {

                ...

            }

        else

            {

                ...

            }

and il works fine.

However, how can I investigate the reason (if any) because a snapshot was not created?

Regards

marius

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

You could try to look at the corresponding Task object, that normally contains the error.

Not sure if it will show the real reason of the failure though.

You can get to the Task via the TaskEvent

$my_snapshot = new-snapshot -VM $my_vm -name $snapshot_neme -Description $description

if ($my_snapshot)


  {


  ...


  }


else


  {

   $snapTask = Get-VIEvent -Entity (Get-VM -Name $my_vm) -Start (Get-Date).AddMinutes(-5) |

   where{$_ -is [VMware.Vim.TaskEvent]} |

  Sort-Object -Property CreatedTime -Descending |

  Select -First 1

   Get-View -Id $snapTask.Info.Task

  }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
rwk1982
Enthusiast
Enthusiast

You could use try...catch for this:

try {

     new-snapshot -VM $my_vm -name $snapshot_neme -Description $description -ErrorAction Stop

} catch {

     $_.Exception

}

In this Case you must add the "-ErrorAction Stop" to the Command because the default Error Action ($ErrorActionPreference) in PowerShell is set to "Continue" and try...catch needs "Stop" to work.

Drink coffee.. Do stupid things faster with more energy...
Reply
0 Kudos