{"id":666,"date":"2023-12-07T15:49:00","date_gmt":"2023-12-07T15:49:00","guid":{"rendered":"http:\/\/draith.com\/?p=666"},"modified":"2024-02-16T03:52:47","modified_gmt":"2024-02-16T03:52:47","slug":"quick-code-install-ama-and-assign-a-dcr-with-powershell","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=666","title":{"rendered":"Quick Code &#8211; Install AMA and Assign a DCR with PowerShell"},"content":{"rendered":"\n<p>Happy Holidays!  Here&#8217;s a quick post to share some code that will inventory Azure VMs, install the AMA if necessary, and then assign a DCR to the VM.  <\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\"># Ensure you're logged in to Azure\nConnect-AzAccount\n\n# Define the Data Collection Rule (DCR) resource ID\n$dcrResourceId = &quot;&lt;Your-DCR-Resource-ID&gt;&quot;\n\n# Get all VMs in the subscription\n$vms = Get-AzVM\n\n# Use ForEach-Object with -Parallel to process VMs concurrently\n$vms | ForEach-Object -Parallel {\n    $vm = $_\n    $osType = $vm.StorageProfile.OsDisk.OsType\n    $extensionName = if ($osType -eq &quot;Windows&quot;) { &quot;AzureMonitorWindowsAgent&quot; } else { &quot;AzureMonitorLinuxAgent&quot; }\n    $extensionPublisher = &quot;Microsoft.Azure.Monitor&quot;\n    $vmResourceId = &quot;\/subscriptions\/$using:vm.SubscriptionId\/resourceGroups\/$using:vm.ResourceGroupName\/providers\/Microsoft.Compute\/virtualMachines\/$using:vm.Name&quot;\n\n    try {\n        # Check if the Azure Monitor Agent extension is installed\n        $amaExtension = Get-AzVMExtension -ResourceGroupName $using:vm.ResourceGroupName -VMName $using:vm.Name -Name $extensionName -ErrorAction SilentlyContinue\n\n        if (-not $amaExtension) {\n            try {\n                # Install the Azure Monitor Agent extension\n                Set-AzVMExtension -ResourceGroupName $using:vm.ResourceGroupName -VMName $using:vm.Name -Name $extensionName -Publisher $extensionPublisher -ExtensionType $extensionName -TypeHandlerVersion &quot;1.0&quot; -Location $using:vm.Location\n                Write-Host &quot;Installed Azure Monitor Agent on $($using:vm.Name)&quot;\n            } catch {\n                Write-Host &quot;Failed to install Azure Monitor Agent on $($using:vm.Name): $_&quot;\n            }\n        } else {\n            Write-Host &quot;Azure Monitor Agent is already installed on $($using:vm.Name)&quot;\n        }\n    } catch {\n        Write-Host &quot;Error checking Azure Monitor Agent on $($using:vm.Name): $_&quot;\n    }\n\n    try {\n        # Assign the DCR to the VM\n        $settings = @{ &quot;dataCollectionRuleResourceIds&quot; = @($using:dcrResourceId) }\n        Set-AzVMExtension -ResourceGroupName $using:vm.ResourceGroupName -VMName $using:vm.Name -Name &quot;AzureMonitorVmExtension&quot; -Publisher $extensionPublisher -ExtensionType $extensionName -Settings $settings -Location $using:vm.Location\n        Write-Host &quot;Assigned DCR to $($using:vm.Name)&quot;\n    } catch {\n        Write-Host &quot;Failed to assign DCR to $($using:vm.Name): $_&quot;\n    }\n} -ThrottleLimit 5 # Adjust the ThrottleLimit as necessary\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Happy Holidays! Here&#8217;s a quick post to share some code that will inventory Azure VMs, install the AMA if necessary, and then assign a DCR to the VM.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,27,16],"class_list":["post-666","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-azure-monitor","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=666"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions\/667"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}