TEE14 Scripted Demo 2 – Live Exporting & Cloning Hyper-V Virtual Machines

The second demo in my presentation focused on being able to export running virtual machines. We can also export a checkpoint to create a merged export. And then we can import a VM to clone it, maybe for troubleshooting, diagnostics, performance testing, upgrade testing, and rollback testing …. all on a “production” VM with “production” data and services.

This script will do:

  1. Clean up the lab
  2. Show the running VM
  3. Export the VM
  4. Show the export
  5. Remove the export
  6. Checkpoint the VM
  7. Export the checkpoint
  8. Import the checkpoint to create a new VM
  9. Highlight the new VM is running alongside the old VM

CLS
$DemoVM1 = “NUMA”
$ExportPath = “D:\Exports\”
$ImportedVMName = “Newly Imported VM”
$ImportVMPath = “D:\Virtual Machines\$ImportedVMName”

#Clean up the demo
Start-VM $DemoVM1 | Out-Null
CLS
If (Test-Path $ExportPath)
{
Remove-Item $ExportPath -Recurse -Force | Out-Null
}
Remove-VMSnapshot $DemoVM1 -ErrorAction Ignore | Out-Null
Stop-VM $ImportedVMName -Force -ErrorAction Ignore | Out-Null
Remove-VM $ImportedVMName -Force -ErrorAction Ignore | Out-Null
Remove-Item $ImportVMPath -Recurse -Confirm:$false -ErrorAction Ignore | Out-Null

#Start the demo
Read-Host “Start the demo”
Write-Host “`nThis is the virtual machine $DemoVM that we will be working with” -foregroundcolor red -backgroundcolor yellow
Get-VM $DemoVM1 | Select Name, Status | Out-Host

#Export the VM
Read-Host “`nExport the running VM”
Write-Host “`nCreating an export of the virtual machine $DemoVM while it is running” -foregroundcolor red -backgroundcolor yellow
Write-Host “`nExport-VM $DemoVM1 -Path $ExportPath”
Export-VM $DemoVM1 -Path $ExportPath | Out-Host
Write-Host “`nHere is the export of the still running virtual machine” -foregroundcolor red -backgroundcolor yellow
Dir $ExportPath\NUMA

#Create a VM checkpoint
Read-Host “`nCreate a checkpoint of the VM $DemoVM1”
Write-Host “`nCreating a checkpoint (formerly known as a snapshot) of the virtual machine $DemoVM1” -foregroundcolor red -backgroundcolor yellow
Write-Host “`nCheckpoint-VM $DemoVM1 -SnapshotName `”Demo Checkpoint AKA Snapshot`””
Checkpoint-VM $DemoVM1 -SnapshotName “Demo Checkpoint AKA Snapshot”
Write-Host “`nThis is the new checkpoint” -foregroundcolor red -backgroundcolor yellow
Get-VMSnapshot $DemoVM1 | Out-Host

 

#Export the VM checkpoint
Read-Host “`nDo an export of the VM $DemoVM1 checkpoint”
If (Test-Path $ExportPath)
{
Remove-Item $ExportPath -Recurse -Force | Out-Null
}
Write-Host “`nWe can export a checkpoint of a running virtual machine” -foregroundcolor red -backgroundcolor yellow

Write-Host “`nNew-Item -ItemType Directory $ExportPath\$DemoVM1”
New-Item -ItemType Directory $ExportPath\$DemoVM1

Write-Host “`nExport-VMSnapshot -Name “Demo Checkpoint AKA Snapshot” -VMName $DemoVM1 -Path $ExportPath”
Export-VMSnapshot -Name “Demo Checkpoint AKA Snapshot” -VMName $DemoVM1 -Path $ExportPath | Out-Host

Write-Host “`nHere is the export” -foregroundcolor red -backgroundcolor yellow
Dir $ExportPath\NUMA

#Import the VM checkpoint to create a new VM
Read-Host “`nImport the exported checkpoint to create a new VM”
Write-Host “`nNow we will create a whole new virtual machine from the exported checkpoint” -foregroundcolor red -backgroundcolor yellow

Write-Host “`n`$XML = gci `”$ExportPath$DemoVM1\Virtual Machines`” | Where-Object {$_.Extension -eq `”.XML`”}”
$XML = gci “$ExportPath$DemoVM1\Virtual Machines” | Where-Object {$_.Extension -eq “.XML”}

Write-Host “`n`$NewVM = IMPORT-VM -path `$XML.FullName -Copy -VhdDestinationPath `”$ImportVMPath\Virtual Hard Disks`” -VirtualMachinePath `”$ImportVMPath`” -SnapshotFilePath `”$ImportVMPath\Snapshots`” -SmartPagingFilePath `”$ImportVMPath`” -GenerateNewId”
$NewVM = IMPORT-VM -path $XML.FullName -Copy -VhdDestinationPath “$ImportVMPath\Virtual Hard Disks” -VirtualMachinePath $ImportVMPath -SnapshotFilePath “$ImportVMPath\Snapshots” -SmartPagingFilePath $ImportVMPath -GenerateNewId

Write-Host “`nRename-VM `$NewVM $ImportedVMName”
Rename-VM $NewVM $ImportedVMName

Write-Host “`nStart-VM $ImportedVMName”
Start-VM $ImportedVMName

Write-Host “`nHere is the original virtual machine $DemoVM1 and the new virtual machine $ImportedVMName” -foregroundcolor red -backgroundcolor yellow
Get-VM $ImportedVMName,$DemoVM1 | Select Name, Status | Out-Host

#Clean up the demo
Read-Host “`nEnd the demo”
Start-VM $DemoVM1 | Out-Null
If (Test-Path $ExportPath)
{
Remove-Item $ExportPath -Recurse -Force | Out-Null
}
CLS
Remove-VMSnapshot $DemoVM1 -ErrorAction Ignore | Out-Null
Stop-VM $ImportedVMName -Force -ErrorAction Ignore | Out-Null
Remove-VM $ImportedVMName -Force -ErrorAction Ignore | Out-Null
Remove-Item $ImportVMPath -Recurse -Confirm:$false -ErrorAction Ignore | Out-Null

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.