开发者

Setting Hyper-V snapshot's name programmatically

I'm creating an Hyper-V snapshot with a C# program:

    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;
    }

(NOTE: This code was taken from MSDN)

Is there a way to set the snapshot name through this WMI call ? Otherwise, does anyone know a working solution to rename a snapshot through WMI call? I already found this thread, but it'开发者_StackOverflows some kind of ambiguous and it doesn't provide any solution ...

EDIT: The solution was to rename snapshot after having create it. Here's my function to rename the snapshot using Hans advice:

SOLUTION:

    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;
    }


You have to use the ModifyVirtualSystem method of the Msvm_VirtualSystemManagementService class to rename a hyper-v snapshot. There is a MSDN example on how to rename a hyper-v virtual machine (You have to modify the code in order to rename a snapshot). Furthermore I've found this example on how to rename a hyper-v snapshot. Hope, this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜