Virtual Business Card


A Jack of all trades is a master of integration, as such an individual who knows enough from many learned trades and skills to be able to bring their disciplines together in a practical manner, and is not a specialist but can be an expert in many fields. Such a person is known as a polymath or a renaissance man; a typical example is someone like Leonardo da Vinci.

Sunday, February 21, 2010

Powershell Script to Backup VMs in Hyper-V: A Poor Man's Alternative to DPM

The script below was taken from the blog: http://mindre.net/post/Powershell-script-for-snapshot-and-exporting(backup)-Virtual-Machines.aspx . I've tweaked it a bit by adding error handling, making sure that the restoring of the VM state is done prior to processing the next VM and writing the results in the event log:


##
## Create a backup of all the vm's
##

$dest = "F:\Backup"
$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService
$ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName <> Name"

[system.diagnostics.eventlog]::WriteEntry("VM Backup","Back-up Process Started",[system.diagnostics.eventlogentrytype]::Information)
foreach ($VM in [array] $ListOfVMs)
{
$VMReturnState = $VM.EnabledState
$VMName = $VM.ElementName
try
{
if (($VM.EnabledState -eq 2) -or ($VM.EnabledState -eq 32768) -or ($VM.EnabledState -eq 32770))
{
$VM.RequestStateChange(32769)
echo "Saving the state of $VMName"
}
while (!($VM.EnabledState -eq 32769) -and !($VM.EnabledState -eq 3))
{
Start-Sleep(1)
$VM = get-wmiobject -namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName='$VMName'"
}
if ([IO.Directory]::Exists("$dest\TmpDir\$VMName"))
{
[IO.Directory]::Delete("$dest\TmpDir\$VMName", $True)
}
echo "Exporting the VM"
$status = $VM_Service.ExportVirtualSystem($VM.__PATH, $True, "$dest\TmpDir")
if ($status.ReturnValue -eq 4096)
{
$job = [Wmi]$status.Job
while (!($job.PercentComplete -eq 100) -and ($job.ErrorCode -eq 0))
{
Start-Sleep(5)
$job = [Wmi]$status.Job
echo $job.PercentComplete
}
}
## Store the files on in a temp directory before moving them to their location and then remove the old files.
if ([IO.Directory]::Exists("$dest\$VMName"))
{
[IO.Directory]::Move("$dest\$VMName", "$dest\$VMName-OldTmpDir")
[IO.Directory]::Move("$dest\TmpDir\$VMName", "$dest\$VMName")
[IO.Directory]::Delete("$dest\$VMName-OldTmpDir", $True)
}
else
{
[IO.Directory]::Move("$dest\TmpDir\$VMName", "$dest\$VMName")
}
$VM.RequestStateChange($VMReturnState)
while (!($VM.EnabledState -eq $VMReturnState))
{
Start-Sleep(1)
$VM = get-wmiobject -namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName='$VMName'"
}
echo "Done with $VMName"
[system.diagnostics.eventlog]::WriteEntry("VM Backup","Back-up Virtual Machine $VMName complete",[system.diagnostics.eventlogentrytype]::Information)
}catch [SystemException] {
echo "Error backing-up $VMName"
[system.diagnostics.eventlog]::WriteEntry("VM Backup","Error backing-up virtual machine: $VMName",[system.diagnostics.eventlogentrytype]::Error)
}
}
[system.diagnostics.eventlog]::WriteEntry("VM Backup","Back-up Process Complete",[system.diagnostics.eventlogentrytype]::Information)

You can use notepad or PowerGui to modify PowerShell Scripts

No comments:

Post a Comment