HyperV Prevented Starting Virtual Machine Because it is Prepared For Failover #AzureSiteRecovery #ASR

| , ,

Published on | Updated on December 30, 2020

3 Min. Read

Hello folks,

I was recently cleaning my Azure Site Recovery failover demo at the Azure ITCamp and I hit the following error:

An error occurred while attempting to start the selected virtual machine. Hyper-V prevented starting virtual machine because it is prepared for planned failover. Virtual machine can be started after cancelling failover preparation”.

HyperV-ASR-StartVM-02

Now before I start with the root cause and the solution of this error, here is what my demo looks like:

  • Single Hyper-V host registered with Azure Site Recovery Vault.
  • Single Hyper-V site to group one or more Hyper-V servers located in a physical location.
  • Single Protection group to group together virtual machines that have common protection requirements.
  • Two tier application (VMs) protected with ASR (Back-end SQL database and front-end web server).
  • Single Recovery Plan to shutdown and startup virtual machines in sequence as the following: (Shutdown: 1- Web Server / 2-SQL DB – Startup: 1-SQL DB / 2- Web Server).

During my demo, I initiated a planned failover which took around 34 minutes…

If you want to know what Azure Site Recovery does behind the scene, here is the job details:

HyperV-ASR-StartVM-09

As you can see, the planned failover completed successfully!

During the planned failover process, I lost network connectivity between my Hyper-V site and Azure, what I ended up with a Virtual Machine on-premises with “Prepared for planned failover”.

HyperV-ASR-StartVM-04

HyperV-ASR-StartVM-05

The virtual machines are in off state, because Azure Site Recovery during the planned failover operation will initiate a final synch before shutting down the VMs on-premise.

If you are familiar with Hyper-V Replica technology, ASR use the same technology.

During the cleaning process, I started deleting the virtual machines and Hyper-V server from the Azure Site Recovery portal without paying attention on the status on–premise, this causes the VM not to start because it’s still in prepared for failover state, however the failover to Azure was completed.

HyperV-ASR-StartVM-01

There is no option to remove the replication for this VM from Hyper-V Manager, because when a VM is protected using ASR, all the operations must be done from the Azure Site Recovery portal and not from Hyper-V Manager.

What about PowerShell…

HyperV-ASR-StartVM-06

The error is a little bit different here… The operation not allowed because the virtual machine is replicating to a provider other Hyper-V.

Who is the other provider? … ASR of course… The UI error was more precise in this case.

Now in order to manual unregister the Hyper-V server and disable the protection, you want to run the following script on the Hyper-V server where the VM reside.

pushd .
try
{
     $windowsIdentity=[System.Security.Principal.WindowsIdentity]::GetCurrent()
     $principal=new-object System.Security.Principal.WindowsPrincipal($windowsIdentity)
     $administrators=[System.Security.Principal.WindowsBuiltInRole]::Administrator
     $isAdmin=$principal.IsInRole($administrators)
     if (!$isAdmin)
     {
        "Please run the script as an administrator in elevated mode."
        $choice = Read-Host
        return;       
     }

    $error.Clear()    
    "This script will remove the old Azure Site Recovery Provider related properties. Do you want to continue (Y/N) ?"
    $choice =  Read-Host

    if (!($choice -eq 'Y' -or $choice -eq 'y'))
    {
    "Stopping cleanup."
    return;
    }

    $serviceName = "dra"
    $service = Get-Service -Name $serviceName
    if ($service.Status -eq "Running")
    {
        "Stopping the Azure Site Recovery service..."
        net stop $serviceName
    }

    $asrHivePath = "HKLM:\SOFTWARE\Microsoft\Azure Site Recovery"
    $registrationPath = $asrHivePath + '\Registration'
    $proxySettingsPath = $asrHivePath + '\ProxySettings'
    $draIdvalue = 'DraID'

    if (Test-Path $asrHivePath)
    {
        if (Test-Path $registrationPath)
        {
            "Removing registration related registry keys."  
            Remove-Item -Recurse -Path $registrationPath
        }

        if (Test-Path $proxySettingsPath)
    {
            "Removing proxy settings"
            Remove-Item -Recurse -Path $proxySettingsPath
        }

        $regNode = Get-ItemProperty -Path $asrHivePath
        if($regNode.DraID -ne $null)
        {            
            "Removing DraId"
            Remove-ItemProperty -Path $asrHivePath -Name $draIdValue
        }
        "Registry keys removed."
    }

    # First retrive all the certificates to be deleted
    $ASRcerts = Get-ChildItem -Path cert:\localmachine\my | where-object {$_.friendlyname.startswith('ASR_SRSAUTH_CERT_KEY_CONTAINER') -or $_.friendlyname.startswith('ASR_HYPER_V_HOST_CERT_KEY_CONTAINER')}
    # Open a cert store object
    $store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My","LocalMachine")
    $store.Open('ReadWrite')
    # Delete the certs
    "Removing all related certificates"
    foreach ($cert in $ASRcerts)
    {
        $store.Remove($cert)
    }
}catch
{   
    [system.exception]
    Write-Host "Error occured" -ForegroundColor "Red"
    $error[0] 
    Write-Host "FAILED" -ForegroundColor "Red"
}
popd

HyperV-ASR-StartVM-07

This script will remove the old Azure Site Recovery Provider and all related settings on the server.

After removing Azure Site Recovery Provider from the server, we need to clean up the protection settings for the VM manually.

On the source affected Hyper-V host server, to remove replication for the virtual machine use this script. Replace $vmName with the name of your virtual machine.

$vmName = "VM-T02"
$vm = Get-WmiObject -Namespace "root\virtualization\v2" -Query "Select * From Msvm_ComputerSystem Where ElementName = '$vmName'"
$replicationService = Get-WmiObject -Namespace "root\virtualization\v2"  -Query "Select * From Msvm_ReplicationService"
$replicationService.RemoveReplicationRelationship($vm.__PATH)

HyperV-ASR-StartVM-08

This script will remove the replication settings for the affected VM by querying the WMI Namespace “root\virtualization\v2“, then query the “Msvm_ReplicationService” class and remove the replication relationship.

The VM will start now and the replication is manually removed Smile

HyperV-ASR-StartVM-10

Reference: Azure Site Recovery Documentation.

Hope this helps!

Enjoy your weekend…

Cheers,
-Charbel

Previous

Update Rollup 9 for System Center 2012 R2 is Now Available #SysCtr #SystemCenter #SCVMM

Free Whitepaper! Understand Microsoft Hyper-Converged Solution #WS2016 #HyperV #S2D

Next

Let me know what you think, or ask a question...

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

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized! You can copy from the 'Code Blocks' in Black.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!