重命名vmware快照

时间:2017-02-06 04:34:34

标签: c#-4.0 vmware

我有Windows桌面应用程序,可以命令或自动执行VMware功能。我想重命名现有快照并使用以下流程进行更新,但我没有看到任何vmware API重命名现有快照。任何人都可以提供有关如何重命名vmware快照的信息吗? 感谢

  

点击“注册并启动快照” - >启动访客VM   关机 - >重命名当前快照-SS1至Temp-SS1->拍摄新快照   使用当前快照名称(即SS1) - >删除以前重命名的快照   快照(即Temp-SS1)。

1 个答案:

答案 0 :(得分:0)

以上两种方法可以简单地完成:

public bool CreateSnapShot(string vmName, string snapShotName, string snapShotDescription, bool replaceSnapShot, string currentSnapShotName)
    {
        try
        {
            NameValueCollection filter = new NameValueCollection();
            filter.Add(Constants.VM_FILTER_NAME, vmName);

            ManagedObjectReference snapShotMor = null;
            VirtualMachine vmObject = (VirtualMachine) vimClient.FindEntityViews(typeof(VirtualMachine), null, filter, null).FirstOrDefault();
            if (vmObject != null)
            {
                if (vmObject.Runtime.PowerState == VirtualMachinePowerState.poweredOn)
                    vmObject.ShutdownGuest();
                while (vmObject.Runtime.PowerState == VirtualMachinePowerState.poweredOn)
                {
                    Thread.Sleep(5000);
                    vmObject.UpdateViewData();//This will refresh VM object state
                }

                if (replaceSnapShot && currentSnapShotName!= "")
                {
                    if (RenameSnapshot(snapShotName, vmObject))
                        snapShotMor = vmObject.CreateSnapshot(snapShotName, snapShotDescription, false, false);
                }
                else snapShotMor = vmObject.CreateSnapshot(snapShotName, snapShotDescription, false, false);

                if (snapShotMor != null)
                    return true;
                else return false;
            }
            else return false;
        }
        catch (Exception ex)
        {
            return false;
        }           
    }

快照重命名可以通过以下方法完成:

public bool RenameSnapshot(string snapShotName, VirtualMachine vmObject)
    {
        try
        {                                                 
             ManagedObjectReference snapshotObject = vmObject.Snapshot.CurrentSnapshot;
             VirtualMachineSnapshot currentSnapshotName = new VirtualMachineSnapshot(vimClient, snapshotObject);
             currentSnapshotName.RenameSnapshot("Temp-" + snapShotName, "Renamed for deletion");

            return true;
        }

        catch (Exception ex)
        {
            return false;
        }
    }

完成上述两个步骤后,您可以删除重命名的(临时)快照。