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


  • Hi 

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

    referrence by :

    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);
                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))
                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"]);
                        Console.WriteLine("Failed to export VM");
                else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
                    Console.WriteLine("VM '{0}' were exported successfully.", vm["ElementName"]);
                    Console.WriteLine("Export virtual system failed with error:{0}", outParams["ReturnValue"]);
            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:

    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


  • 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

    25 Haziran 2012 Pazartesi 16:00