Hyper-V: Export VM Config-Only Using PowerShell

Hyper-V: Export VM Config-Only Using PowerShell

Read about configuration-only (config-only) export of your VMs on this blog post: http://blogs.technet.com/b/virtualization/archive/2009/05/29/hyper-v-r2-import-export-part-6-so-what-happened-to-configuration-only-export.aspx

Here is a sample PowerShell script to export VM config-only:

 
$ns = 'root\virtualization'
$expDir = 'd:\vmexports'
 
#Get VM Object
$vm = gwmi -n $ns Msvm_ComputerSystem | ?{$_.ElementName -eq 'VM'}
 
#Get export setting object
$exp = @($vm.GetRelated('Msvm_VirtualSystemExportSettingData'))[0]
 
#If you dont want to copy the VHDs and AVHDs
$exp.CopyVmStorage = $false
 
#If you dont want to copy the Saved state
$exp.CopyVmRuntimeInformation = $false
 
#Get VMMS object
$vmms = gwmi -n $ns Msvm_VirtualSystemManagementService
 
#Perform Export
$out = $vmms.ExportVirtualSystemEx($vm.Path.Path, $expDir, $exp.GetText(1))
 
#Perform Job handling if necessary
if ($out.ReturnValue -eq 4096)
{
       $task = [Wmi]$out.Job;
       while($task.JobState -eq 3 -or $task.JobState -eq 4)
       {
              $task.Get();
              sleep 1;
       }
       if ($task.JobState -ne 7)
       {
              "Error exporting VM " + $task.ErrorDescription;
       }            
       else
       {
              "Export completed successfully..."
       }     
      
}
elseif ($out.ReturnValue -ne 0)
{
       "Export failed with error : " + $out.ReturnValue;
}
else
{
       "Export completed successfully..."
}
 

 

Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 2. Comment: Replace RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 1. Comment: Removed (en-US) from title, modified title casing

  • Craig Lussier edited Original. Comment: added en-US to tags and title

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Craig Lussier edited Original. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 1. Comment: Removed (en-US) from title, modified title casing

  • Richard Mueller edited Revision 2. Comment: Replace RGB values with color names in HTML to restore colors

Page 1 of 1 (3 items)