none
Exporting the Configuration of a Virtual Machine failed in C# programme.

    Soru

  • Hi 

    I try to export the configuration of Hyper-V Virtual Machine by C# programme.

    referrence by :

    http://msdn.microsoft.com/en-us/library/ee355220(v=vs.85).aspx

    below is my code:

    class ExportVirtualSystemExConfigOnlyClass
        {
            static string GetConfigOnlyVirtualSystemExportSettingDataInstance(ManagementScope scope)
            {
                ManagementPath settingPath = new ManagementPath("Msvm_VirtualSystemExportSettingData");
                ManagementClass exportSettingDataClass = new ManagementClass(scope, settingPath, null);
                ManagementObject exportSettingData = exportSettingDataClass.CreateInstance();
                // Do not copy VHDs and AVHDs but copy the Snapshot configuration and Saved State information (Runtime information) if present
                exportSettingData["CopySnapshotConfiguration"] = 0;
                exportSettingData["CopyVmRuntimeInformation"] = true;
                exportSettingData["CopyVmStorage"] = false;
                exportSettingData["CreateVmExportSubdirectory"] = true;
                string settingData = exportSettingData.GetText(TextFormat.CimDtd20);
                exportSettingData.Dispose();
                exportSettingDataClass.Dispose();
                return settingData;
            }
            static void ExportVirtualSystemExConfigOnly(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);
                inParams["ComputerSystem"] = vm.Path.Path;
                if (!Directory.Exists(exportDirectory))
                {
                    Directory.CreateDirectory(exportDirectory);
                }
                inParams["ExportDirectory"] = exportDirectory;
                inParams["ExportSettingData"] = GetConfigOnlyVirtualSystemExportSettingDataInstance(scope);
                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: ExportVirtualSystemEx vmName exportDirectory");
                   // return;
                //}
                string vmName = "Win2008-HyperV";
                string exportDirectory = "E:\\configout";
                ExportVirtualSystemExConfigOnly(vmName, exportDirectory);
            }
        }

    "Wind2008-HyperV" is my Hyper-V hostname. I want to export the configuration to "E:\configout".

    I get the Common Utilities from: 

    http://msdn.microsoft.com/en-us/library/cc723869(v=vs.85).aspx

    When I invoke the method ExportVirtualSystemEx of Msvm_VirtualSystemManagementService at this line: 

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

    I always get the return code 32768 which means excute failed.

    The result of this programme is "Export virtual system failed with error:32768".

    How to fix this issue?

    21 Haziran 2012 Perşembe 08:30

Yanıtlar

  • Hi, 

    The VBscript in the reference worked properly on my Hyper-V computer. Please ensure that you run the C# codes properly.

    By the way, as this is a script in MSDN site, it is recommend you to get further support in the corresponding community so that you can get the most qualified pool of respondents. Thanks for your understanding.
     
    For your convenience, I have list the related link as followed.
     
    MSDN Forums
    http://social.msdn.microsoft.com/Forums/en/categories

    25 Haziran 2012 Pazartesi 16:00