Hi ,
I'm trying to write a small utility to export a virtual machine on Windows hyperV. I tried this program using Visual studio 2002 and select Console app / .net framework 4.8. I found this example microsoft website.
Here is the link: learn.microsoft.com/en-gb/previous-versions/windows/desktop/virtual/msvm-virtualsystemmanagementservice-exportvirtualsystemex
I cannot get anything under Utility.GetServiceObject or Utility.GetTargetComputer(vmName, scope).
Any help will be highly appriciated.
Following is the full code from MS:
using System; using System.IO; using System.Management; namespace HyperVSamples { class ExportVirtualSystemExClass { static string GetVirtualSystemExportSettingDataInstance(ManagementScope scope) { ManagementPath settingPath = new ManagementPath("Msvm_VirtualSystemExportSettingData"); ManagementClass exportSettingDataClass = new ManagementClass(scope, settingPath, null); ManagementObject exportSettingData = exportSettingDataClass.CreateInstance(); exportSettingData["CopySnapshotConfiguration"] = 0; exportSettingData["CopyVmRuntimeInformation"] = true; exportSettingData["CopyVmStorage"] = true; exportSettingData["CreateVmExportSubdirectory"] = true; string settingData = exportSettingData.GetText(TextFormat.CimDtd20); exportSettingData.Dispose(); exportSettingDataClass.Dispose(); return settingData; } static void ExportVirtualSystemEx(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"] = GetVirtualSystemExportSettingDataInstance(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; } ExportVirtualSystemEx(args[0], args[1]); } } }