@Le Vinh Dat Thanks for sharing the script.
Summarizing from the discussion (above). @Le Vinh Dat, Thanks for sharing the info/solution with the community.
Issue: Unable to delete Physical machine in Azure Site Recovery
Error: Machine is already protected by Azure Site Recovery
Solution: Resolved by Azure support "Need to run some PowerShell scripts to update properties of the servers in Azure physical site."
Steps to be followed:
Go to your VM, Follow below steps:
- ArmClient has to be pre-installed. (https://github.com/projectkudu/ARMClient/releases )
- Open the PowerShell window. Run the login command.
ARMClient.exe login
- Get the recovery services fabric details using below commands. Replace the subscription and vault details.
.\ARMClient.exe get /Subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.RecoveryServices/vaults/<vaultName>/replicationFabrics?api-version=2021-06-01
- Pick the physicalSiteId from the above results. To get the site details, machine details and machine list run the below commands.
.\ARMClient.exe get "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>?api-version=2020-09-09-preview"
.\ARMClient.exe get "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>/machines?api-version=2020-09-09-preview"
- Get the Physical site Id and machine Id from the above commands output. And run below script to remove the protected tag on that machine:
$armId = "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>"
$apiInput = @{}
$tags = New-Object System.Collections.Generic.Dictionary"[String,String]"
$machines = New-Object System.Collections.ArrayList
$machine = @{}
$machine['machineArmId'] = "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>/machines/<machineId>"
$machine['tags'] = $tags
$machines.Add($machine)
$apiInput['machines'] = $machines
$armCmd = $armId + "/UpdateProperties?api-version=2020-09-09-preview"
$apiInput | ConvertTo-Json -Depth 5 | armclient.exe post $armCmd -verbose
Then go back and see your server is available for replication again.Go to your VM, Follow below steps:
- ArmClient has to be pre-installed. (https://github.com/projectkudu/ARMClient/releases )
- Open the PowerShell window. Run the login command.
ARMClient.exe login
- Get the recovery services fabric details using below commands. Replace the subscription and vault details.
.\ARMClient.exe get /Subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.RecoveryServices/vaults/<vaultName>/replicationFabrics?api-version=2021-06-01
- Pick the physicalSiteId from the above results. To get the site details, machine details and machine list run the below commands.
.\ARMClient.exe get "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>?api-version=2020-09-09-preview"
.\ARMClient.exe get "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>/machines?api-version=2020-09-09-preview"
- Get the Physical site Id and machine Id from the above commands output. And run below script to remove the protected tag on that machine:
$armId = "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>"
$apiInput = @{}
$tags = New-Object System.Collections.Generic.Dictionary"[String,String]"
$machines = New-Object System.Collections.ArrayList
$machine = @{}
$machine['machineArmId'] = "/subscriptions/<subscritionId>/resourceGroups/<rgName>/providers/Microsoft.OffAzure/ServerSites/<physicalsite>/machines/<machineId>"
$machine['tags'] = $tags
$machines.Add($machine)
$apiInput['machines'] = $machines
$armCmd = $armId + "/UpdateProperties?api-version=2020-09-09-preview"
$apiInput | ConvertTo-Json -Depth 5 | armclient.exe post $armCmd -verbose
Then go back and see your server is available for replication again.
Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer. They can only accept answers by others, I' have summarized and posted the solution/answer to benefit the community users to find the answers quickly.
If the response helped, do "Accept Answer" and up-vote it