Here are some examples of how you can use Hyper-V Replica to solve problems that you might encounter.
The company you are working for has completed an upgrade from Windows Server 2008 R2 to Windows Server 2012 and has converted a large number of virtual machines from VMware vSphere. This has been done so that the company can use Hyper-V Replica as a DR solution. There are 300 virtual machines to configure. You need to complete the following:
You can rule out using the GUI if there are 300 virtual machines to configure. The best solution is to use Microsoft Excel to document the required configuration for each virtual machine and save the document as a CSV file. There will be one line per virtual machine. The CSV file can then be read in, one line at a time, to configure HVR for each virtual machine. A time delay can be built into the script to slow down the process.
Create a CSV file that looks like the following example. It will serve two purposes. First, it will document the DR plan for each virtual machine. Second, it will be used by a PowerShell script to enable Hyper-V Replica.
VM,ReplicaHost,Port,Authentication,History,VSSFreq
VM01,host3.demo.internal,80,Kerberos,0,0
VM02,host3.demo.internal,80,Kerberos,15,2
VM03,host3.demo.internal,80,Kerberos,7,3
Note that if History is 0, there will be no recovery points, and if VSSFreq is 0, application-consistent snapshots will not be created. The following script will read this CSV file and implement HVR for you:
#Configure sleep timer
$SleepInSeconds = 300
Import-CSV C:ScriptsReplica.csv | foreach {
#Will there be recovery points?
if ($_.History -eq 0)
{
#No recovery points
Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication
}
else
{
#Will VSS be used?
if ($_.History -eq 0)
{
#No VSS
Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication `
-RecoveryHistory $_.History
}
else
{
#VSS
Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication `
-RecoveryHistory $_.History -VSSSnapshotFrequency $_.VSSFreq
}
}
Start-VMInitialReplication -VMName $_.VM
Write-Host "$_.VM :: Replication configured"
Write-Host "Sleeping for $SleepInSeconds seconds"
Start-Sleep -s $SleepInSeconds
}
You have 300 virtual machines running in your production site. There is news of a hurricane bringing floods that will affect the area. You need to invoke your DR plan as quickly as possible so that the following can occur:
How will you do this work as quickly as possible?
There are two parts to the planned failover. The first part is to initiate the planned failover on the production hosts. The following script will find each replicating source virtual machine, power it down, and initiate a planned failover. The script should be run on each host in the production site:
Get-VM | Where {$_.ReplicationMode -eq "Primary"} | ForEach-Object {
Stop-VM $_.Name -Force
Start-VMFailover -VMName $_.Name -Prepare -Confirm:$false
}
Write-Host "First half of planned failover is complete. Please continue in `
DR site"
The second part of the process has to be run on each host in the DR site. This script will complete the failover, reverse replication, and start up every replica virtual machine found on the host:
Get-VM | Where {$_.ReplicationMode -eq "Replica"} | ForEach-Object {
Start-VMFailover -VMName $_.Name -Confirm:$false
Set-VMReplication -reverse -VMName $_.Name
Start-VM $_.Name
}
Write-Host "Planned failover and replication reverse is complete."
Import-CSV C:ScriptsOrderedFailover.csv | foreach {
Start-VMFailover -VMName $_.VM -Confirm:$false
Set-VMReplication -reverse -VMName $_.VM
Start-VM $_.VM
}
Write-Host "Planned ordered failover and replication reverse is complete."
You have been asked to create a solution that will orchestrate the unplanned failover of virtual machines in the DR site using a PowerShell script. The script must start up each virtual machine in order and record if the virtual machine was able to successfully start up within a predetermined amount of time. This time out will vary from one virtual machine to another. There could be large number of virtual machines running on several hosts.
This solution is going to use a PowerShell script. The script will
The following is an example CSV file, saved as C:ScriptsFailover.txt:
Host,VM,Timeout
Host2,VM01,10
Host2,VM02,60
Host2,VM03,10
This is the script:
Function Check-VM ($CheckHost, $CheckVM, [int]$CheckTimeout)
{
#Set this variable so it works in the following while loop
$TheVM = $CheckVM
Write-Output "Checking $TheVM Now"
Write-Output "Timeout is $CheckTimeout seconds"
#Configure a time out
$LoopTimeout = new-timespan -Seconds $CheckTimeout
$SW = [Diagnostics.StopWatch]::StartNew()
#Keep checking the VM while the time out hasn't expired
While ($SW.elapsed -lt $LoopTimeout){
#Check the VM's integration component heartbeat
$HeartBeat = Get-VMIntegrationService -ComputerName $CheckHost -VMName `
$TheVM -Name Heartbeat -ErrorAction Continue
If ($HeartBeat.PrimaryStatusDescription -Eq "OK")
{
Write-Output "$TheVM is running"
#The VM is running so we can break out of the loop
Break
}
Else
{
Write-Output "Waiting on $TheVM ... "
#The VM is not yet running so sleep 5 seconds before checking again
sleep 5
}
}
#The script only gets here if the time out has expired
if ($HeartBeat.PrimaryStatusDescription -Ne "OK")
{
If ((Get-VM $TheVM -ComputerName Host2).State -EQ "Running") `
{ Stop-VM -ComputerName $CheckHost $TheVM -Force }
#The VM is not responding to the integration heartbeat so we time out and
stop the VM
write-output "$TheVM Timed out"
}
#End of function
}
#The unplanned failover script starts here
cls
#Define the comma seperated values (CSV) file
$CSVFile = "C:ScriptsFailover.txt"
#In this script, there are 3 columns in the CSV file:
# - Host: The name of the host/cluster that the VM replica is on
# - VM: The name of the virtual machine being failed over
# - Timeout: How long (in seconds) should the script wait for the VM to be online
Import-CSV $CSVFile | ForEach {
$FailHost = $_.Host
$FailVM = $_.VM
$FailTimeout = $_.TimeOut
If ((Get-VM -ComputerName $FailHost $FailVM).ReplicationMode -EQ "Replica")
{
Write-Output "Failing over $FailVM on $FailHost now"
Start-VMFailover -ComputerName $FailHost -VMName $FailVM -Confirm:$false
Start-VM -ComputerName $FailHost -VMName $FailVM
Check-VM $FailHost $FailVM $FailTimeout
}
else
{
Write-Output "$FailVM on $FailHost is not ready for failover"
}
}
This script could be made more powerful by adding some functionality such as the following:
18.118.162.201