//Copied from samples VMSnapshot private VirtualMachineSnapshotInfo getSnapshotInfo (ManagedObjectReference vmmor, String vmName) { ObjectContent[] snaps = cb.getServiceUtil().GetObjectProperties( null, vmmor, new String[] { "snapshot" } ); VirtualMachineSnapshotInfo snapInfo = null; if (snaps != null && snaps.Length > 0) { ObjectContent snapobj = snaps[0]; DynamicProperty[] snapary = snapobj.propSet; if (snapary != null && snapary.Length > 0) { snapInfo = ((VirtualMachineSnapshotInfo)(snapary[0]).val); } } else { Console.WriteLine("No Snapshots found for VirtualMachine : " + vmName); } return(snapInfo); }
private Boolean listSnapshot(ManagedObjectReference vmMor) { ObjectContent[] snaps = cb.getServiceUtil().GetObjectProperties( null, vmMor, new String[] { "snapshot" }); VirtualMachineSnapshotInfo snapInfo = null; if (snaps != null && snaps.Length > 0) { ObjectContent snapobj = snaps[0]; DynamicProperty[] snapary = snapobj.propSet; if (snapary != null && snapary.Length > 0) { snapInfo = ((VirtualMachineSnapshotInfo)(snapary[0]).val); VirtualMachineSnapshotTree[] snapTree = snapInfo.rootSnapshotList; traverseSnapshotInTree(snapTree, null, true); } else { Console.WriteLine("No Snapshots found"); return(false); } } return(true); }
public string DeleteSnapshot(P2PBackup.Common.Node vm, string snapMoref) { string deleteTask = null; ObjectContent[] oCary = GetObjectProperties("VirtualMachine", new string[] { "snapshot" }, null /*GetVmMoRef(vm)*/); ObjectContent oc = null; ManagedObjectReference mor = null; ManagedObjectReference snapMor = null; DynamicProperty[] pcary = null; DynamicProperty pc = null; for (int oci = 0; oci < oCary.Length; oci++) { oc = oCary[oci]; mor = oc.obj; pcary = oc.propSet; if (mor.type != "VirtualMachine" || pcary == null) { continue; } for (int i = 0; i < pcary.Length; i++) { pc = pcary[i]; if (pc.val is VirtualMachineSnapshotInfo) { VirtualMachineSnapshotInfo snapInfo = (VirtualMachineSnapshotInfo)pc.val; ManagedObjectReference theSnapMor = snapInfo.currentSnapshot; if (theSnapMor.Value == snapMoref) { snapMor = theSnapMor; break; } } } if (snapMor != null) { break; } } if (snapMor != null) { if (LogEvent != null) { LogEvent(this, new LogEventArgs(0, Severity.INFO, "Deleting snapshot '" + snapMor.Value + "' for virtual node " + vm.ToString())); } try{ deleteTask = service.RemoveSnapshot_Task(snapMor, true).Value; } catch {} // timed-out getting vsphere response, ignore. } return(deleteTask); }
static ManagedObjectReference GetSnapshot(string targetvm, bool takeNew) { if (takeNew) { VirtualMachineRuntimeInfo runtime = GetProperty <VirtualMachineRuntimeInfo>(vm, "runtime"); if (runtime.powerState.ToString() != "poweredOn") { Error(new Exception("VM is not powered on, no point snapshotting")); } Log("[x] Creating snapshot for VM " + targetvm + "..."); ManagedObjectReference task = vim.CreateSnapshot_Task(vm, "System Backup " + DateTime.Now.ToString(), "System Backup" + DateTime.Now.ToString(), true, true); string state = GetProperty <TaskInfo>(task, "info").state.ToString(); while (state != "success") { switch (state) { case "error": Error(new Exception("Error creating snapshot")); break; case "running": Thread.Sleep(10000); break; } state = GetProperty <TaskInfo>(task, "info").state.ToString(); } Log("[x] Snapshot created successfully"); return((ManagedObjectReference)GetProperty <TaskInfo>(task, "info").result); } else { Log("[x] Finding existing snapshots for " + targetvm + "..."); VirtualMachineSnapshotInfo snapshotInfo = null; try { snapshotInfo = GetProperty <VirtualMachineSnapshotInfo>(vm, "snapshot"); } catch (Exception e) { Error(new Exception("No existing snapshots found for the VM " + targetvm + ", recommend you try again with --snapshot set")); } return(snapshotInfo.currentSnapshot); } }
//Copied from samples VMSnapshot private ManagedObjectReference getSnapshotReference(ManagedObjectReference vmmor, String vmName, String snapName) { VirtualMachineSnapshotInfo snapInfo = getSnapshotInfo(vmmor, vmName); ManagedObjectReference snapmor = null; if (snapInfo != null) { VirtualMachineSnapshotTree[] snapTree = snapInfo.rootSnapshotList; snapmor = traverseSnapshotInTree(snapTree, snapName, false); } else { Console.WriteLine("No Snapshot named : " + snapName + " found for VirtualMachine : " + vmName); } return(snapmor); }