Exporting a Snapshot of a Virtual Machine
The following C# and VBScript samples demonstrate exporting a snapshot of a virtual machine (VM). Samples that demonstrate exporting a virtual machine can be found on the ExportVirtualSystemEx topic.
C#: The referenced utilities can be found in Common Utilities for the Virtualization Samples.
using System;
using System.IO;
using System.Management;
namespace HyperVSamples
{
class ExportVirtualSystemExSnapshotsClass
{
static ManagementObject GetLastVirtualSystemSnapshot(ManagementObject vm)
{
ManagementObjectCollection settings = vm.GetRelated(
"Msvm_VirtualSystemsettingData",
"Msvm_PreviousSettingData",
null,
null,
"SettingData",
"ManagedElement",
false,
null);
ManagementObject virtualSystemsetting = null;
foreach (ManagementObject setting in settings)
{
Console.WriteLine(setting.Path.Path);
Console.WriteLine(setting["ElementName"]);
virtualSystemsetting = setting;
}
return virtualSystemsetting;
}
static string GetVirtualSystemExportSettingDataInstance(ManagementScope scope, ManagementObject snapshotSettingData)
{
ManagementPath settingPath = new ManagementPath("Msvm_VirtualSystemExportSettingData");
ManagementClass exportSettingDataClass = new ManagementClass(scope, settingPath, null);
ManagementObject exportSettingData = exportSettingDataClass.CreateInstance();
exportSettingData["CopySnapshotConfiguration"] = 2;
exportSettingData["CreateVmExportSubdirectory"] = true;
exportSettingData["SnapshotVirtualSystem"] = snapshotSettingData.Path.Path;
string settingData = exportSettingData.GetText(TextFormat.CimDtd20);
exportSettingData.Dispose();
exportSettingDataClass.Dispose();
return settingData;
}
static void ExportVirtualSystemExSnapshots(string vmName, string exportDirectory)
{
ManagementScope scope = new ManagementScope(@"root\virtualization", null);
ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("ExportVirtualSystemEx");
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObject snapshotSettingData = GetLastVirtualSystemSnapshot(vm);
if ( snapshotSettingData == null )
{
Console.WriteLine("There are not snapshots in the VM to be exported...");
return;
}
inParams["ComputerSystem"] = vm.Path.Path;
if (!Directory.Exists(exportDirectory))
{
Directory.CreateDirectory(exportDirectory);
}
inParams["ExportDirectory"] = exportDirectory;
inParams["ExportSettingData"] = GetVirtualSystemExportSettingDataInstance(scope, snapshotSettingData);
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("ExportVirtualSystemEx", inParams, null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine("VM '{0}' were exported successfully.", vm["ElementName"]);
}
else
{
Console.WriteLine("Failed to export VM");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine("VM '{0}' were exported successfully.", vm["ElementName"]);
}
else
{
Console.WriteLine("Export virtual system failed with error:{0}", outParams["ReturnValue"]);
}
inParams.Dispose();
outParams.Dispose();
vm.Dispose();
virtualSystemService.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: ExportVirtualSystemExSnapshots vmName exportDirectory");
return;
}
ExportVirtualSystemExSnapshots(args[0], args[1]);
}
}
}
VB |
---|
|
Related topics