Azure Site recovery clearing locks

%3CLINGO-SUB%20id%3D%22lingo-sub-1498814%22%20slang%3D%22en-US%22%3EAzure%20Site%20recovery%20clearing%20locks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1498814%22%20slang%3D%22en-US%22%3E%3CP%3ERecently%20had%20an%20issue%20in%20re-protecting%20VM's%20following%20failover%20to%20a%20secondary%20region.%20Tried%20everything%20and%20couldn't%20resolve%20eventually%20after%20speaking%20with%20Azure%20support%20i%20ran%20the%20below%20script%20and%20resolved%20the%20issue%20with%20locks%20not%20clearing%20on%20failed%20over%20VM's%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecls%3C%2FP%3E%3CP%3E%24subscriptionId%20%3D%20Read-Host%20'What%20is%20your%20Azure%20Subscription%20ID%3F'%3CBR%20%2F%3E%24rgName%20%3D%20Read-Host%20'Specify%20the%20name%20of%20the%20Resource%20group%20in%20which%20your%20VM%20is%20located'%3CBR%20%2F%3E%24vmName%20%3D%20Read-Host%20'Specify%20the%20name%20of%20the%20VM'%3C%2FP%3E%3CP%3E%23%20Sign-in%20with%20Azure%20account%20credentials%3CBR%20%2F%3ELogin-AzureRmAccount%3C%2FP%3E%3CP%3E%23%20Select%20Azure%20Subscription%3CBR%20%2F%3ESelect-AzureRmSubscription%20-SubscriptionId%20%24subscriptionId%3C%2FP%3E%3CP%3E%23%20Remove%20any%20locks%3CBR%20%2F%3E%24locks%20%3D%20Get-AzureRmResourceLock%20-ResourceGroupName%20%24rgName%20-ResourceName%20%24vmName%20-ResourceType%20Microsoft.Compute%2FvirtualMachines%3CBR%20%2F%3Eif%20(%24locks%20-ne%20%24null%20-and%20%24locks.Count%20-ge%200)%7B%3CBR%20%2F%3E%24canDelete%20%3D%20Read-Host%20'The%20VM%20has%20locks%20that%20could%20prevent%20cleanup%20of%20Azure%20Site%20Recovery%20stale%20links%20left%20from%20previous%20protection.%20Do%20you%20want%20the%20locks%20deleted%20to%20ensure%20cleanup%20goes%20smoothly%3F%20Reply%20with%20Y%2FN.'%3CBR%20%2F%3E%3CBR%20%2F%3Eif%20(%24canDelete.ToLower()%20-eq%20%22y%22)%7B%3CBR%20%2F%3EForeach%20(%24lock%20in%20%24locks)%20%7B%3CBR%20%2F%3E%24lockId%20%3D%20%24lock.LockId%3CBR%20%2F%3ERemove-AzureRmResourceLock%20-LockId%20%24lockId%20-Force%3CBR%20%2F%3EWrite-Host%20%22Removed%20Lock%20%24lockId%20for%20%24vmName%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%24linksResourceId%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fmanagement.azure.com%2Fsubscriptions%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmanagement.azure.com%2Fsubscriptions%2F%3C%2FA%3E'%20%2B%20%24subscriptionId%20%2B%20'%2Fproviders%2FMicrosoft.Resources%2Flinks'%3CBR%20%2F%3E%24vmId%20%3D%20'%2Fsubscriptions%2F'%20%2B%20%24subscriptionId%20%2B%20'%2FresourceGroups%2F'%20%2B%20%24rgName%20%2B%20'%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2F'%20%2B%20%24vmName%20%2B%20'%2F'%3C%2FP%3E%3CP%3EWrite-Host%20%24(%22Deleting%20links%20for%20%24vmId%20using%20resourceId%3A%20%24linksResourceId%22)%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%24links%20%3D%20%40(Get-AzureRmResource%20-ResourceId%20%24linksResourceId%7C%20Where-Object%20%7B%24_.Properties.sourceId%20-match%20%24vmId%20-and%20%24_.Properties.targetId.ToLower().Contains(%22microsoft.recoveryservices%2Fvaults%22)%7D)%3CBR%20%2F%3EWrite-Host%20%22Links%20to%20be%20deleted%22%3CBR%20%2F%3E%24links%3C%2FP%3E%3CP%3E%23Delete%20all%20links%20which%20are%20of%20type%3CBR%20%2F%3E%3CBR%20%2F%3EForeach%20(%24link%20in%20%24links)%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3EWrite-Host%20%24(%22Deleting%20link%20%22%20%2B%20%24link.Name)%3CBR%20%2F%3ERemove-AzureRmResource%20-ResourceId%20%24link.ResourceId%20-Force%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%24links%20%3D%20%40(Get-AzureRmResource%20-ResourceId%20%24linksResourceId%7C%20Where-Object%20%7B%24_.Properties.sourceId%20-match%20%24vmId%20-and%20%24_.Properties.targetId.ToLower().Contains(%22%2Fprotecteditemarmid%2F%22)%7D)%3CBR%20%2F%3EWrite-Host%20%22Cross%20subscription%20Links%20to%20be%20deleted%22%3CBR%20%2F%3E%24links%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%23Delete%20all%20links%20which%20are%20of%20type%3CBR%20%2F%3EForeach%20(%24link%20in%20%24links)%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3EWrite-Host%20%24(%22Deleting%20link%20%22%20%2B%20%24link.Name)%3CBR%20%2F%3ERemove-AzureRmResource%20-ResourceId%20%24link.ResourceId%20-Force%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3EWrite-Host%20%24(%22Deleted%20all%20links%20%22)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1498814%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ELocks%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESite%20Recovery%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Regular Visitor

Recently had an issue in re-protecting VM's following failover to a secondary region. Tried everything and couldn't resolve eventually after speaking with Azure support i ran the below script and resolved the issue with locks not clearing on failed over VM's

 

cls

$subscriptionId = Read-Host 'What is your Azure Subscription ID?'
$rgName = Read-Host 'Specify the name of the Resource group in which your VM is located'
$vmName = Read-Host 'Specify the name of the VM'

# Sign-in with Azure account credentials
Login-AzureRmAccount

# Select Azure Subscription
Select-AzureRmSubscription -SubscriptionId $subscriptionId

# Remove any locks
$locks = Get-AzureRmResourceLock -ResourceGroupName $rgName -ResourceName $vmName -ResourceType Microsoft.Compute/virtualMachines
if ($locks -ne $null -and $locks.Count -ge 0){
$canDelete = Read-Host 'The VM has locks that could prevent cleanup of Azure Site Recovery stale links left from previous protection. Do you want the locks deleted to ensure cleanup goes smoothly? Reply with Y/N.'

if ($canDelete.ToLower() -eq "y"){
Foreach ($lock in $locks) {
$lockId = $lock.LockId
Remove-AzureRmResourceLock -LockId $lockId -Force
Write-Host "Removed Lock $lockId for $vmName"
}
}
}

$linksResourceId = 'https://management.azure.com/subscriptions/' + $subscriptionId + '/providers/Microsoft.Resources/links'
$vmId = '/subscriptions/' + $subscriptionId + '/resourceGroups/' + $rgName + '/providers/Microsoft.Compute/virtualMachines/' + $vmName + '/'

Write-Host $("Deleting links for $vmId using resourceId: $linksResourceId")


$links = @(Get-AzureRmResource -ResourceId $linksResourceId| Where-Object {$_.Properties.sourceId -match $vmId -and $_.Properties.targetId.ToLower().Contains("microsoft.recoveryservices/vaults")})
Write-Host "Links to be deleted"
$links

#Delete all links which are of type
Foreach ($link in $links)

{
Write-Host $("Deleting link " + $link.Name)
Remove-AzureRmResource -ResourceId $link.ResourceId -Force
}


$links = @(Get-AzureRmResource -ResourceId $linksResourceId| Where-Object {$_.Properties.sourceId -match $vmId -and $_.Properties.targetId.ToLower().Contains("/protecteditemarmid/")})
Write-Host "Cross subscription Links to be deleted"
$links


#Delete all links which are of type
Foreach ($link in $links)

{
Write-Host $("Deleting link " + $link.Name)
Remove-AzureRmResource -ResourceId $link.ResourceId -Force
}

Write-Host $("Deleted all links ")

0 Replies