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
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
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
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.