Hyper-V PowerShell VM Export
Author: jason
Date: 2010-10-23 09:15:42
Category: Technical

Virtual Machines are a good way to consolidate servers on one Host Platform. But putting your eggs in a single Hyper-V basket means you have to be more careful with backups.

My solution was to find and implement a PowerShell script that would detect, shutdown, export, and re-start the running Virtual Machines in Hyper-V.

We need to Export the Virtual Machines via Hyper-V for an effective and simple backup. Just copying/pasting the VHD files isn't as simple as it was with VirtualPC or other Virtual Machine products. Performing an Export via Hyper-V creates a config.xml for each VM. That let's us take all those VM files and put them on another Hyper-V server. If I save the Export files to a File Server, and my Hyper-V crashes, I can quickly put those exported VMs onto another Hyper-V and start them up.

You could use Windows Backup, but that is an All Or Nothing backup solution. It does let you backup with the VM running, reference: http://support.microsoft.com/kb/958662. I prefer to have my VMs saved off individually, as opposed to lumped together in the backup. My current setup lets me power down the VMs each week without impacting users. You could also buy Microsoft's Data Protection Manager, and if you already have Microsoft's Virtual Machine Manager that is probably what you would do.

Hyper-V
http://www.microsoft.com/hyper-v-server/en/us/default.aspx
"Microsoft® Hyper-V™ Server 2008 R2 is a stand-alone product that provides a reliable and optimized virtualization solution enabling organizations to improve server utilization and reduce costs. Since Hyper-V Server is a dedicated stand-alone product, which contains only the Windows Hypervisor, Windows Server driver model and virtualization components, it provides a small footprint and minimal overhead. It easily plugs into customers’ existing IT environments, leveraging their existing patching, provisioning, management, support tools, processes, and skills. Some of the new key new features that are available in Microsoft Hyper-V Server 2008 R2 are live migration, cluster shared volume support and expanded processor and memory support for host systems."

Virtual Machine Manager
http://www.microsoft.com/systemcenter/en/us/virtual-machine-manager.aspx
"Virtual Machine Manager 2008 R2 helps enable centralized management of physical and virtual IT infrastructure, increased server utilization, and dynamic resource optimization across multiple virtualization platforms. It includes end-to-end capabilities such as planning, deploying, managing, and optimizing the virtual infrastructure."

Data Protection Manager
http://www.microsoft.com/systemcenter/en/us/data-protection-manager.aspx
"Data Protection Manager (DPM) 2010 is part of the System Center family of management products from Microsoft. It delivers unified data protection for Windows servers such as SQL Server, Exchange, SharePoint, Virtualization and file servers -- as well as Windows desktops and laptops. DPM seamlessly uses disk, tape, and cloud-based repositories to deliver an easy-to-use and best-of-breed backup and recovery solution for Windows environments from Microsoft. Windows customers of all sizes can rely on Microsoft to provide a scalable and manageable protection solution that is cost-effective, secure, and reliable."

Introducing PowerShell
http://msdn.microsoft.com/en-us/library/aa973757(VS.85).aspx
"Windows PowerShell is a new Windows command-line shell designed especially for system administrators. The Windows PowerShell includes an interactive prompt and a scripting environment that can be used independently or in combination. Unlike most shells, which accept and return text, Windows PowerShell is built on top of the .NET Framework common language runtime (CLR) and the .NET Framework, and accepts and returns .NET Framework objects. This fundamental change in the environment brings entirely new tools and methods to the management and configuration of Windows."

If running Virtual Machine Manager, here is how to script the database backup: http://technet.microsoft.com/en-us/library/dd446635.aspx

Couple notes about PowerShell
1) Script files (commandlets) end with the extension .ps1
2) To run unsigned commandlets you can issue the following command and type Y for yes: set-executionpolicy remotesigned
3) Run the PowerShell prompt as administrator
4) To get to PowerShell you can go to Start - Run and type powershell or go through Programs - Accessories - Windows PowerShell, and then select Windows PowerShell

To use Task Scheduler to schedule the script
1) Use the Program path to point to PowerShell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
2) Use the Add Arguements to point to the VMBackup script -Command ".'c:\vmbackup.ps1'"

VMBackup.ps1


#PowerShell script
#VMBackup.ps1
#Intent: Read VMs, Shutdown running VM, Export, Re-Start, Repeat
#Two items to change: Hyper-V server name and Backup path
#Original script from: http://circulotito.blogspot.com/2008/06/hyper-v-virtual-machine-backup.html

# Hyper-V Server Name
$SERVER_HOST = "MyServer"

$ListofVMs = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter "ElementName <> Name"

foreach ($VM in $ListofVMs)
{
# Check VM status, echo back VM not running
If ($VM.EnabledState -ne 2)
{
echo "VM Not Running, skipping"
}

#Check VM status, if its running shutdown
If ($VM.EnabledState -eq 2)
{
# ************************
# Shutdown
# ************************
$query = "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" + $VM.name + "'"
$Shutdown = get-wmiobject -query $query -namespace "root\virtualization" -computername "."

#Request a forced shutdown
$Result = $Shutdown.InitiateShutdown($true,"Backup periodico")

$VMName = "ElementName = '" + $VM.ElementName + "'"

while ($VM.EnabledState -ne 3)
{
sleep(1)
$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $VMName
}
# ***************************
# Export
# ***************************
$VM_Service = get-wmiobject -computer $SERVER_HOST –namespace root\virtualization –class Msvm_VirtualSystemManagementService

$MyDate = get-date

#Change Backup Path
$VMBackupDir = "E:\Backups\" + "Backup_" + $MyDate.year + "_" + $MyDate.month + "_" + $MyDate.day

$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, $VMBackupDir)

If ($status.ReturnValue -eq 4096)
{
# Get the WMI instance of the object
$Job = [WMI]$Status.Job
$JobStatus = $Job.JobState
while ($JobStatus -ne 7)
{
sleep(1)
# Get the WMI instance of the object
$Job = [WMI]$Status.Job
$JobStatus = $Job.JobState
}
}

# ***************************
# Startup
# ***************************
$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $VMName
If ($VM.EnabledState -eq 3)
{
$VM.RequestStateChange(2)
while ($VM.EnabledState -ne 2)
{
sleep(1)
$VM = get-wmiobject -computer $SERVER_HOST -namespace root\virtualization -class Msvm_ComputerSystem -filter $VMName
}
}
}
}





jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009