Issue with Enable Replication for Azure Site Recovery
Please refer old Issue ID #4866
@viverm @shahabhijeet @markcowl
I am having issue with Azure Site Recovery rest api . Authentication header is already added in my script and its working with "GET" API requests . But I have this particular error case in the attached screenshot that I am facing for enabling replication From VMware (on-premise) to Azure in the mobility service installation and preparation step as well as in Unplanned Failover at the Shutdown Source VM step.
I am able to enable the replication from Portal GUI as well. But Rest API calls are not working for this scenario. Is the Current APIs does not support this functionality?
Its a kind request please don't close the issue until there is a satisfactory resolution is available for this issue.This automation is very critical for me. I will be thankful if you guys could provide me your techinical advise and support. If don't have the resolution that's OK.You can close the issue.
Please find the attached script .
Enable-ReplicationVMwareToAzure.txt

hi @shubhamgupta514
in the attached txt file I am seeing two function ARM-VMInfoAPI and RESTAPI-Auth
looks like ASR-EnableReplicationAPI is not present in file. Can you please add that method too ..
Might be this can help .. Sample Agent-Authentication
{ "NotBeforeTimestamp": "xxxxxxxxxxxxxx", "NotAfterTimestamp": "xxxxxxxxxxxxxxx", "ClientRequestId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "HashFunction": "HMACSHA256", "Hmac": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Version": { "Major": 1, "Minor": 2, "Build": -1, "Revision": -1, "MajorRevision": -1, "MinorRevision": -1 }, "PropertyBag": { } }
See the logic of Hmac generation .. and how you can get channel integrity key for onprem operations.

Generate Agent authentication header : from https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/RecoveryServices.SiteRecovery/Commands.RecoveryServices.SiteRecovery/Common/PSAsrClient.cs
https://msdn.microsoft.com/en-us/library/system.version(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
How to get the Channel Integrity key to be used for the hash:-
GET https://management.azure.com/Subscriptions/
The response body will contain a field called the integrity key. This is your channel integrity key.
@shubhamgupta514 any update on this ?
@viverm I made the changes in the script as per your recommendations above. But this time I am getting different error while invoking the rest-api .Please look into the attached updated script and let me know if anything is still missing.
Enable-ReplicationVMwareToAzure-v1.1.txt
############################################################### #Error ###############################################################
Invoke-RestMethod : {"error":{"code":"BadRequest","message":"The operation has failed.","details":[{"code":"InvalidChannelIntegrityKeyToken","message":"The operation has failed.","possibleCauses":"An internal server error occurred.","recommendedAction":"Retry the last action. If the issue persists contact support.","clientRequestId":"2e5733ef-b519-449d-bed4-f997b440408b","activityId":"dfd040ad-496d-46da-a515-c6f8becfaa05"}]}} At C:\Users\shubham.k.gupta\Downloads\Enable-ReplicationVMwareToAzure.ps1:64 char:31
- ... ationInfo = Invoke-RestMethod -Method Put -Headers $requestheader -Ur ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
@viverm - Is there any update on this? Please help me out to close this ASAP.
@viverm - Did you take a look at the new error?
Use javascript serializer
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$shaInput = $ser.serialize($global:cikTokenDetails)
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($integrityKey)
$global:cikTokenDetails.Hmac = [Convert]::ToBase64String($hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($shaInput)))
$global:cikTokenDetails.HashFunction = "HMACSHA256"
@viverm
I am getting below error while executing Invoke-Restmethod.
Please find the updated script below:
Enable-ReplicationVMwareToAzure-v1.2.txt
######################################################################## #Error ##########################################################################
Invoke-RestMethod : {"error":{"code":"BadRequest","message":"The operation has failed.","details":[{"code":"InvalidChannelIntegrityKeyToken","message":"The operation has failed.","possibleCauses":"An internal server error occurred.","recommendedAction":"Retry the last action. If the issue persists contact support.","clientRequestId":"38c03514-0374-44a6-aba0-3275a1565d6c","activityId":"c180569e-c132-4b07-a35d-ac4240769ee6"}]}} At line:48 char:31
- ... ationInfo = Invoke-RestMethod -Method Put -Headers $requestheader -Ur ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
@viverm please advise
@markcowl @viverm could you please advise me on this
@shubhamgupta514 we have added support for VMware to Azure Site recovery from powershell .
You can use those cmdlet directly .
@viverm - Thanks for the update.I have checked new module version and its working for me.
But I am still curious to understand how can I make this work with the help of rest api functionality.
@shubhamgupta514 I tried some dry on run code added . It is working fine for me .
It just need more investigation what is going wrong. Powershell code is available on git you cake take the reference from it.
closing thread . Not active from long time.