Hyper-V R2 Import/Export – Part 4 – Export code sample
Published Mar 21 2019 02:53 PM 197 Views
Brass Contributor
First published on TECHNET on May 27, 2009

In the following sample, we do a basic export of a VM using the new Export API. Here we export a VM with all the data associated with it. In a later blog, I will cover configuration-only export. Many thanks to Madhan Gajendran and Dinesh Kumar Govindasamy from the Hyper-V team for writing this script:



<br/><br/><br/><br/><br/><br/><br/><br/><br/>.csharpcode, .csharpcode pre<br/>{<br/> font-size: small;<br/> color: black;<br/> font-family: consolas, "Courier New", courier, monospace;<br/> background-color: #ffffff;<br/> /*white-space: pre;*/<br/>}<br/>.csharpcode pre { margin: 0em; }<br/>.csharpcode .rem { color: #008000; }<br/>.csharpcode .kwrd { color: #0000ff; }<br/>.csharpcode .str { color: #006080; }<br/>.csharpcode .op { color: #0000c0; }<br/>.csharpcode .preproc { color: #cc6633; }<br/>.csharpcode .asp { background-color: #ffff00; }<br/>.csharpcode .html { color: #800000; }<br/>.csharpcode .attr { color: #ff0000; }<br/>.csharpcode .alt <br/>{<br/> background-color: #f4f4f4;<br/> width: 100%;<br/> margin: 0em;<br/>}<br/>.csharpcode .lnum { color: #606060; }



option explicit  dim objWMIServicedim managementServicedim fileSystem const JobStarting = 3const JobRunning = 4const JobCompleted = 7const wmiStarted = 4096const wmiSuccessful = 0 Main()  '-----------------------------------------------------------------' Main'-----------------------------------------------------------------Sub Main()  dim computer, objArgs, vmName, vm, exportDirectory set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject") computer = "." set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization") set managementService = objWMIService.ExecQuery("select * from Msvm_VirtualSystemManagementService").ItemIndex(0) set objArgs = WScript.Arguments if WScript.Arguments.Count = 2 then vmName = objArgs.Unnamed.Item(0) exportDirectory = objArgs.Unnamed.Item(1) else WScript.Echo "usage: cscript ExportVirtualSystemEx.vbs vmName exportDirectoryName" WScript.Quit(1) end if set vm = GetComputerSystem(vmName)  if ExportVirtualSystemEx(vm, exportDirectory) then WriteLog "Done" WScript.Quit(0) else WriteLog "ExportVirtualSystemEx Failed." WScript.Quit(1) end ifEnd Sub '-----------------------------------------------------------------' Retrieve Msvm_VirtualComputerSystem from base on its ElementName'-----------------------------------------------------------------Function GetComputerSystem(vmElementName) On Error Resume Next dim query query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName) set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0) if (Err.Number <> 0) then WriteLog Format1("Err.Number: {0}", Err.Number) WriteLog Format1("Err.Description:{0}",Err.Description) WScript.Quit(1) end ifEnd Function '-----------------------------------------------------------------' Export a virtual system'-----------------------------------------------------------------Function ExportVirtualSystemEx(computerSystem, exportDirectory)  dim objInParam, objOutParams dim query dim computer dim exportSettingData ExportVirtualSystemEx = false if Not fileSystem.FolderExists(exportDirectory) then fileSystem.CreateFolder(exportDirectory) end if set objInParam = managementService.Methods_("ExportVirtualSystemEx").InParameters.SpawnInstance_() objInParam.ComputerSystem = computerSystem.Path_.Path  query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_VirtualSystemExportSettingData", computerSystem.Path_.Path) set exportSettingData = objWMIService.ExecQuery(query).ItemIndex(0) exportSettingData.CopyVmStorage = true exportSettingData.CopyVmRuntimeInformation = true exportSettingData.CreateVmExportSubdirectory = true exportSettingData.CopySnapshotConfiguration = 0 objInParam.ExportSettingData = exportSettingData.GetText_(1)  objInParam.ExportDirectory = exportDirectory set objOutParams = managementService.ExecMethod_("ExportVirtualSystemEx", objInParam)  if objOutParams.ReturnValue = wmiStarted then if (WMIJobCompleted(objOutParams)) then ExportVirtualSystemEx = true end if elseif (objOutParams.ReturnValue = wmiSuccessful) then ExportVirtualSystemEx = true else WriteLog Format1("ExportVirtualSystemEx failed with ReturnValue {0}", objOutParams.ReturnValue) end if End Function '-----------------------------------------------------------------' Handle wmi Job object'-----------------------------------------------------------------Function WMIJobCompleted(outParam) dim WMIJob, jobState  set WMIJob = objWMIService.Get(outParam.Job)  WMIJobCompleted = true  jobState = WMIJob.JobState  while jobState = JobRunning or jobState = JobStarting WriteLog Format1("In progress... {0}% completed.",WMIJob.PercentComplete) WScript.Sleep(1000) set WMIJob = objWMIService.Get(outParam.Job) jobState = WMIJob.JobState wend  if (jobState <> JobCompleted) then WriteLog Format1("ErrorCode:{0}", WMIJob.ErrorCode) WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription) WMIJobCompleted = false end if End Function '-----------------------------------------------------------------' Create the console log files.'-----------------------------------------------------------------Sub WriteLog(line) dim fileStream set fileStream = fileSystem.OpenTextFile(".\ExportVirtualSystemExLog.log", 8, true) WScript.Echo line fileStream.WriteLine line fileStream.Close End Sub '------------------------------------------------------------------------------' The string formating functions to avoid string concatenation.'------------------------------------------------------------------------------Function Format2(myString, arg0, arg1) Format2 = Format1(myString, arg0) Format2 = Replace(Format2, "{1}", arg1)End Function '------------------------------------------------------------------------------' The string formating functions to avoid string concatenation.'------------------------------------------------------------------------------Function Format1(myString, arg0) Format1 = Replace(myString, "{0}", arg0)End Function
Version history
Last update:
‎Mar 21 2019 02:53 PM
Updated by: