Rename a snapshot in Hyper-V WMI V2 from C#

别说谁变了你拦得住时间么 提交于 2019-12-11 23:55:33

问题


I am trying to rename a Hyper-V snapshot (checkpoint) using root\virtualization\v2. None of the standard methods like ModifySystemSettings or ModifyVirtualSystem of Msvm_VirtualSystemSnapshotService or Msvm_VirtualSystemManagementService has been helpful so far.

Powershell Rename-VMSnapshot can do the job however I am not sure it is using WMI.

Any idea?


回答1:


Here is what worked for me:

//
// Rename last snapshot to desired name
//
using (ManagementBaseObject inParams = vmms.GetMethodParameters("ModifySystemSettings"))
{
    ManagementObject setting = null;

    ManagementObjectCollection settings = vm.GetRelated(
        "Msvm_VirtualSystemSettingData",
        "Msvm_MostCurrentSnapshotInBranch",
        null,
        null,
        "Dependent",
        "Antecedent",
        false,
        null
        );

    foreach (ManagementObject instance in settings)
    {
        // Usually only one, but loop through to the end to get latest one

        if (setting != null)
        {
            if (string.Compare(
                    (string)instance["CreationTime"],
                    (string)setting["CreationTime"],
                    true) > 0
                )
            {
                // Get latest one since there could be duplicates
                setting = instance;
            }
        }
        else
        {
            setting = instance;
        }
    }

    setting["ElementName"] = snapshotName;

    inParams["SystemSettings"] = setting.GetText(TextFormat.WmiDtd20);

    using (ManagementBaseObject outParams = vmms.InvokeMethod("ModifySystemSettings", inParams, null))
    {
        // What this does is get Job managementObject and check JobState to be JobCompleted.
        this.ProcessSnapshotMethodResult(outParams, "rename");
    }
}


来源:https://stackoverflow.com/questions/22134084/rename-a-snapshot-in-hyper-v-wmi-v2-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!