Flappy Shoot

Sunday, August 30, 2009

Cloning Virtual machine:

Scenario:
I have two Hyper-V server namely FPDHPV01 and FPDHPV02. FPDHPV01 is running Exchange front end server (FPDEFE01), Blackberry Server (MCNBES01) and Operations Manager (FPDOPM01). We want to schedule the cloning of Operations Manager - FPDOPM01, and followed by other servers regularly for Business Contigency. We dont have good backup solution, so I felt cloning is a short term solution.

The script below is to save the state of running VM (FPDOPM01) in Hyper-V server (FPDHPV02), and clone the virtual machine to target Hyper-V server (FPDHPV01) after removing any existing VM on the target server with the same name.Once the clone is created, we also start the VM (FPDOPM01) from the source Hyper-V server (FPDHPV01) .
SCVMM 2008 is running on FPDHPV01.

You can save the below file as powershell script and change is according to your needs.

$SaveStateSourceVM = Get-VM -VMMServer fpdhpv01.mcnmena.local where { $_.VMHost.Name -eq "fpdhpv02.mcnmena.local" -and $_.Name -eq "FPDOPM01" }

SaveState-VM -VM $SaveStateSourceVM

$VM = Get-VM -VMMServer fpdhpv01.mcnmena.local where { $_.VMHost.Name -eq "fpdhpv01.mcnmena.local" -and $_.Name -eq "FPDOPM01" }

Remove-VM -VM $VM

$VMOrig = Get-VM -VMMServer localhost -Name "FPDOPM01" where {$_.VMHost.Name -eq "fpdhpv02.mcnmena.local"}

$VMHost = Get-VMHost -VMMServer localhost where {$_.Name -eq "fpdhpv01.MCNMENA.LOCAL"}

New-VM -VM $VMOrig -Name "FPDOPM01" -Description "" -Owner "MCNMENA\tariq.admin" -VMHost $VMHost -Path "E:\VHD\FPDOPM01" -StartAction NeverAutoTurnOnVM -StopAction SaveVM

Start-VM $SaveStateSourceVM


I have saved the above powershell cmdlets as FPDOPM01CLONE.PS1 script file. To schedule this job, I have creates a batch file as follows:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft System Center Virtual Machine Manager 2008\bin\cli.psc1" -command "& {C:\Scripts\FPDOPM01Clone.ps1}"

I had a problem executing SCVMM cmdlets even after adding the Microsoft.SystemCenter.VirtualMachineManager Snapin. That is why I have to include PSConsoleFile "C:\Program Files\Microsoft System Center Virtual Machine Manager 2008\bin\cli.psc1" in my batch file while calling powershell.exe

No comments: