Msvm_VirtualSystemSnapshotService 类的 CreateSnapshot 方法

创建虚拟机的快照。

语法

uint32 CreateSnapshot(
  [in]      CIM_ComputerSystem           REF AffectedSystem,
  [in]      string                           SnapshotSettings,
  [in]      uint16                           SnapshotType,
  [in, out] CIM_VirtualSystemSettingData REF ResultingSnapshot,
  [out]     CIM_ConcreteJob              REF Job
);

parameters

AffectedSystem [in]

CIM_ComputerSystem 类的引用,该类表示要创建其快照的虚拟机。

SnapshotSettings [in]

一个字符串,包含CIM_SettingData 类的嵌入实例,该类包含快照的参数设置。 此参数是可选的,可能是空字符串。

SnapshotType [in]

指定请求快照的类型。 这必须是以下值之一。

完整快照 (2)

完成虚拟机快照。

磁盘快照 (3)

虚拟机磁盘的快照。

DMTF 保留 (.)

供应商特定 (32768..65535)

ResultingSnapshot [in, out]

CIM_VirtualSystemSettingData 对象的引用,该对象表示生成的虚拟机快照。

作业 [out]

如果异步执行操作,则此方法将返回 4096,此参数将包含对派生自 CIM_ConcreteJob 的对象的引用。

返回值

此方法返回以下值之一。

已完成,无错误 (0)

不支持 (1)

失败 (2)

超时 (3)

参数 (4) 无效

无效状态 (5)

类型 (6) 无效

DMTF 保留 (.)

方法参数已选中 - 作业已启动 (4096)

方法保留 (4097..32767)

供应商特定 (32768..65535)

示例

以下 C# 示例代码创建虚拟机。 可以在 V2) (虚拟化示例的常用实用工具 中找到引用的实用工具。

重要

若要正常运行,必须在虚拟机主机服务器上运行以下代码,并且必须使用管理员权限运行。

public static void CreateSnapshot(string vmName)
{
    ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
    ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemSnapshotService");

    ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("CreateSnapshot");

    // Set the AffectedSystem property.
    ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
    if (null == vm)
    {
        throw new ArgumentException(string.Format("The virtual machine \"{0}\" could not be found.", vmName));
    }

    inParams["AffectedSystem"] = vm.Path.Path;

    // Set the SnapshotSettings property.
#if(true)
    inParams["SnapshotSettings"] = "";
#else
    ManagementObject snapshotSettings = Utility.GetServiceObject(scope, "CIM_SettingData"); 
    inParams["SnapshotSettings"] = snapshotSettings.ToString();
#endif       
    // Set the SnapshotType property.
    inParams["SnapshotType"] = 2; // Full snapshot.

    ManagementBaseObject outParams = virtualSystemService.InvokeMethod("CreateSnapshot", inParams, null);

    if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
    {
        if (Utility.JobCompleted(outParams, scope))
        {
            Console.WriteLine("Snapshot was created successfully.");

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

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

要求

要求
最低受支持的客户端
Windows 8 [仅限桌面应用]
最低受支持的服务器
Windows Server 2012 [仅限桌面应用]
命名空间
Root\Virtualization\V2
标头
Dbdaoint.h
MOF
WindowsVirtualization.V2.mof
DLL
Vmms.exe

另请参阅

Msvm_VirtualSystemSnapshotService

CreateVirtualSystemSnapshot (V1)