How to deploy Arc Extension Microsoft.Azure.Automation.HybridWorker via REST API

Peter Smit 21 Reputation points
2022-02-02T15:50:27.613+00:00

Hi, I am trying to deploy an Microsoft.Azure.Automation.HybridWorker via powershell and REST API but it fails with the following error:

{"Message":"Specified machineId is not associated with automation account.

This is the method I am used:

$PutHybridRunbookWorkerUri = "https://management.azure.com/subscriptions/$($SubscriptionId)/resourceGroups/$($resourceGroupName)/providers/Microsoft.HybridCompute/machines/$($machineName)/extensions/HybridWorkerExtension?api-version=2021-05-20"
$RequestBodyHybridRunbookWorker = @{
"location" = $($location)
"properties" = @{
"publisher" = "Microsoft.Azure.Automation.HybridWorker"
"type" = "HybridWorkerForWindows"
"typeHandlerVersion" = "0.1.0.18"
"autoUpgradeMinorVersion" = $false
"enableAutomaticUpgrade" = $false
"settings" = @{
"AutomationAccountURL" = $AutomationAccountUrl
}
"protectedSettings" = @{
"primaryaccesskey" = $AutomationAccountKey
}
}
} | ConvertTo-Json -depth 3
Invoke-RestMethod -Headers @{Authorization = "Bearer $token" } -Body $RequestBodyHybridRunbookWorker -Uri $PutHybridRunbookWorkerUri -Method put -ContentType "application/json"

Any ideas?

Kind regards,

Peter Smit

Azure Arc
Azure Arc
A Microsoft cloud service that enables deployment of Azure services across hybrid and multicloud environments.
318 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,112 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,359 questions
{count} votes

Accepted answer
  1. tbgangav-MSFT 10,381 Reputation points
    2022-02-10T08:39:17.06+00:00

    <<Sharing the solution for the benefit of other community members who might be looking for an answer to similar issue>>

    Steps involved to deploy Arc extension HRW (hybrid runbook worker) are:

    1. To create HRW group
    2. To confirm HRW group creation
    3. To generate a GUID and create HRW URI
    4. To confirm HRW creation make a get call
    5. To retrieve Automation Account Hybrid Service URL
    6. To create the extension

    Script:

    $subscriptionId = "" #Automation Account sub id  
    $automationAccountName = "" #Automation account name  
    $resourceGroupName = "" #Automation Account RG  
    $token = (get-azaccesstoken).Token  
    $hybridRunbookWorkerGroupName = "" # HRWG group to be created  
    $ARCSubscriptionId = "" #ARC machine sub id  
    $ARCresourceGroupName = "" #ARC machine RG  
    $ARCmachineName = "" #ARC machine name  
    $ARCMachinelocation = "" # ARC Machine location  
    $ARCServerResourceId = "/subscriptions/$ARCSubscriptionId/resourceGroups/$ARCresourceGroupName/providers/Microsoft.HybridCompute/machines/$ARCmachineName"  
      
    #Update all the above varialbles before using the below script  
      
    #Create HRW Group URI  
    $headers = @{Authorization = "Bearer $token"}  
    $createHRWGroupuri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Automation/automationAccounts/$automationAccountName/hybridRunbookWorkerGroups/$($hybridRunbookWorkerGroupName)?api-version=2021-06-22"  
    $contentType = "application/json"      
    $body = @{} | ConvertTo-Json  
    $response = Invoke-WebRequest -Uri $createHRWGroupuri -Method PUT -Headers $headers -Body $body -ContentType $contentType  
    $response.Content  
      
    #To Confirm HRW Group Creation  
    (Invoke-WebRequest -Uri $createHRWGroupuri -Method Get -Headers $headers).Content  
      
    #Generate HRW id  
    $hrwId = New-Guid  
      
    #Create HRW URI  
    $createHRWuri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Automation/automationAccounts/$automationAccountName/hybridRunbookWorkerGroups/$hybridRunbookWorkerGroupName/hybridRunbookWorkers/$($hrwId)?api-version=2021-06-22"  
      
    $body = @"  
    {  
    "properties":{"vmResourceId": "$ARCServerResourceId"}  
    }  
    "@  
      
    $response = Invoke-WebRequest -Uri $createHRWuri -Method PUT -Headers $headers -Body $body -ContentType $contentType  
    $response.Content  
      
    #To Confirm HRW Creation make a get  
    (Invoke-WebRequest -Uri $createHRWuri -Method Get -Headers $headers).Content   
      
    ##### HRW is not Visible yet in the portal#####  
    #Retrieve Automation Account Hybrid URL  
    $automationAccountInfouri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Automation/automationAccounts/$($automationAccountName)?api-version=2021-06-22"  
    $automationHybridServiceUrl = ((Invoke-WebRequest -Uri $automationAccountInfouri -Method Get -Headers $headers).Content) | ConvertFrom-Json | Select -expand properties | Select -expand automationHybridServiceUrl  
    $automationHybridServiceUrl  
      
    $CreateARCExtensionUri = "https://management.azure.com/subscriptions/$ARCSubscriptionId/resourceGroups/$ARCresourceGroupName/providers/Microsoft.HybridCompute/machines/$ARCmachineName/extensions/HybridWorkerExtension?api-version=2021-05-20"  
    $CreateARCExtensionBody = @{  
        'location'   = $($ARCMachinelocation)  
        'properties' = @{  
            'publisher'               = 'Microsoft.Azure.Automation.HybridWorker'  
            'type'                    = 'HybridWorkerForWindows'  
            'typeHandlerVersion'      = '0.1.0.18'  
            'autoUpgradeMinorVersion' = $false  
            'enableAutomaticUpgrade'  = $false  
            'settings'                = @{  
                'AutomationAccountURL' = $automationHybridServiceUrl  
            }  
        }  
    } | ConvertTo-Json -depth 2  
      
    #Create the Extension  
    Invoke-WebRequest  -Uri $CreateARCExtensionUri -Method PUT -Headers $headers -Body $CreateARCExtensionBody -ContentType $contentType  
    

    Illustration:
    173104-image.png

    173101-image.png

    173027-image.png

    172999-image.png

    172959-image.png

    173083-image.png

    173103-image.png

    173028-image.png

    173111-image.png

    0 comments No comments

0 additional answers

Sort by: Most helpful