I have multiple data collection rule and trying to associate DCR's in Azure Arc machines, but when I am trying using PowerShell foreach loop only last list DCR value is getting associated with azure arc machine.

Dewangan, Vivek Kumar 0 Reputation points
2024-04-15T08:24:06.1366667+00:00

I have multiple data collection rule and trying to associate DCR's in Azure Arc machines, but when I am trying using PowerShell foreach loop only last list DCR value is getting associated with azure arc machine. Other previous arc machines are getting associated but it's get deleted automatically when last DCR list is associated. I even tried this with bicep script it's not working.

I'm using below code :

$getRG = Get-AzResource | Where-Object -FilterScript {$_.ResourceType -eq 'Microsoft.HybridCompute/machines'} | Select-Object ResourceGroupName -Unique
$getRG
Write-Host "RG count: $($getRG.Count)"

$dcrNameList = @($configData.dcrExtentionName,$configData.dcrIISLogsName,$configData.dcrWindowsEventName)
Write-Host "DCR names"
$dcrNameList

foreach($dcr in $dcrNameList){
    Write-Host "dataCollectionRuleAssociations from DCR: $dcr"
    foreach($rg in $getRG){
        Write-Host "ResourceGroup $($rg.ResourceGroupName)"
        $arcVirtualMachines = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType Microsoft.HybridCompute/machines | Select-Object Name
        write-host "VM count : $($arcVirtualMachines.Count)"
        
        foreach($vm in $arcVirtualMachines){

            $associationName = "$($vm.Name)-DCR-association"
            $resourceUri = "/subscriptions/$($configData.subscriptionId)/resourceGroups/$($rg.ResourceGroupName)/providers/Microsoft.HybridCompute/machines/$($vm.Name)"
            $dataCollectionRuleId = "/subscriptions/$($configData.subscriptionId)/resourcegroups/$($configData.resourceGroupName)/providers/Microsoft.Insights/dataCollectionRules/$dcr"

            Write-Host "Associating Arc machine $($vm.Name) to $dcr in ResourceGroup $($rg.ResourceGroupName)"                    
            New-AzDataCollectionRuleAssociation -AssociationName $associationName -ResourceUri $resourceUri -DataCollectionRuleId $dataCollectionRuleId -verbose           
        }
    }

}
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,256 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.