以编程方式设置Hyper-V快照的名称

时间:2011-09-29 14:54:25

标签: c# wmi snapshot hyper-v

我正在使用C#程序创建Hyper-V快照:

    private static bool Snapshot(string vmName, string snapshotName)
    {
        var result = false;
        var scope = new ManagementScope(@"root\virtualization", null);
        var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");

        var vm = Utility.GetTargetComputer(vmName, scope);

        var inParams = virtualSystemService.GetMethodParameters("CreateVirtualSystemSnapshot");
        inParams["SourceSystem"] = vm.Path.Path;

        var outParams = virtualSystemService.InvokeMethod("CreateVirtualSystemSnapshot", inParams, null);

        if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
        {
            if (Utility.JobCompleted(outParams, scope))
            {
                Console.WriteLine("Snapshot was created successfully.");
                result = true;
            }
            else
            {
                Console.WriteLine("Failed to create snapshot VM");
                result = false;
            }
        }
        else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
        {
            Console.WriteLine("Snapshot was created successfully.");
            result = true;
        }
        else
        {
            Console.WriteLine("Create virtual system snapshot failed with error {0}", outParams["ReturnValue"]);
            result = false;
        }

        inParams.Dispose();
        outParams.Dispose();
        vm.Dispose();
        virtualSystemService.Dispose();

        return result;
    }

(注意:此代码取自MSDN

有没有办法通过此WMI调用设置快照名称?否则,是否有人知道通过WMI调用重命名快照的工作解决方案?我已经找到了this thread,但它有些含糊不清,并没有提供任何解决方案......

编辑:解决方案是在创建快照后重命名快照。这是我使用Hans建议重命名快照的功能:

解决方案:

    public static bool RenameSnapshot(string vmName, string snapshotName)
    {

        var result = false;
        var scope = new ManagementScope(@"root\virtualization", null);
        var vm = Utility.GetTargetComputer(vmName, scope);

        // load snapshot
        var objSnapshot = GetLastVirtualSystemSnapshot(vm);

        // rename snapshot
        objSnapshot["ElementName"] = snapshotName;

        // save
        var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
        var inParams = virtualSystemService.GetMethodParameters("ModifyVirtualSystem");
        inParams["ComputerSystem"] = vm.Path.Path;
        inParams["SystemSettingData"] = objSnapshot.GetText(TextFormat.CimDtd20);
        var outParams = virtualSystemService.InvokeMethod("ModifyVirtualSystem", inParams, null);

        if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
        {
            result = true;
        }
        else
        {
            result = false;
        }



        inParams.Dispose();
        outParams.Dispose();
        vm.Dispose();
        virtualSystemService.Dispose();


        return result;
    }

1 个答案:

答案 0 :(得分:2)

您必须使用ModifyVirtualSystem类的Msvm_VirtualSystemManagementService方法重命名hyper-v快照。关于如何重命名hyper-v虚拟机有一个MSDN示例(您必须修改代码才能重命名快照)。此外,我发现了example关于如何重命名超v快照的问题。 希望,这有帮助。