Need script to add workspace id and key in Monitoring Agent in Each VM for 2 Subscriptions

Ram Kumar Cheekoti 161 Reputation points
2023-09-09T04:57:29.32+00:00

Dear Team,

Looking for a script to add workspace ID and key automatically across all the VMs in 2 subscriptions, manually logging into the machine and adding the key either manually or through the script below is a tough task.

Need your help on how I can run the following script across all machines in a single shot.

The below script is taken from the blog :

$workspaceId = "<Your workspace Id>"

$workspaceKey = "<Your workspace Key>"

$mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'

$mma.AddCloudWorkspace($workspaceId, $workspaceKey)

$mma.ReloadConfiguration()

https://learn.microsoft.com/en-us/azure/azure-monitor/agents/agent-manage?tabs=PowerShellLinux#add-or-remove-a-workspace

I am using MAC and Azure CLI and tried but getting the below error, not sure if this is the best way to do. Following the links mentioned here :

https://techcommunity.microsoft.com/t5/itops-talk-blog/how-to-run-scripts-in-your-azure-vm-by-using-run-command/ba-p/1362360

https://stackoverflow.com/questions/64067241/using-az-vm-run-command-from-within-a-ps1-script-and-with-ps1-script-file

User's image

Your help is much appreciated. thank you.

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

Answer accepted by question author
  1. Deepanshu katara 17,960 Reputation points MVP Moderator
    2023-09-09T06:14:40.0233333+00:00

    Hope you are doing good!

    Basically to answer your question , we should use Invoke command to run script in multiple VMs

    Below is a PowerShell script that iterates through VMs in multiple subscriptions and adds the workspace ID and key:

    # Define your workspace ID and key
    $workspaceId = "<Your workspace Id>"
    $workspaceKey = "<Your workspace Key>"
    
    # List your Azure subscriptions
    $subscriptions = Get-AzSubscription
    
    # Loop through each subscription
    foreach ($subscription in $subscriptions) {
        # Set the current subscription context
        Set-AzContext -Subscription $subscription.Id
        
        # Get all VMs in the current subscription
        $vms = Get-AzVM
    
        # Loop through each VM and add the workspace
        foreach ($vm in $vms) {
            # Add the workspace to the VM using the script
            $script = @"
            $mma = New-Object -ComObject 'AgentConfigManager.MgmtSvcCfg'
            $mma.AddCloudWorkspace("$workspaceId", "$workspaceKey")
            $mma.ReloadConfiguration()
    "@
            $result = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -ScriptInline $script -Verbose
            Write-Host "Added workspace to $($vm.Name) in $($subscription.Name)"
        }
    }
    
    Write-Host "Workspace ID and Key added to all VMs in all subscriptions."
    
    
    

    Replace <Your workspace Id> and <Your workspace Key> with your actual workspace ID and key.

    Important Notes:

    • Ensure that the Azure PowerShell module is installed and up to date on your local machine.
    • Make sure you are logged in to Azure using az login with an account that has the necessary permissions to access the VMs and modify them.
    • The script iterates through all VMs in all subscriptions, so be cautious when running it in a production environment.
    • This script uses the Invoke-AzVMRunCommand cmdlet to run the script on the VMs. Ensure that the VM agent is installed and running on the VMs for this to work.

2 additional answers

Sort by: Most helpful
  1. Tushar Kumar 3,381 Reputation points MVP
    2023-09-09T08:01:39.7533333+00:00

    Hi Ram, The problem here is you are using Azure CLI to achieve this and the script you have requires Azure Powershell. You can follow this link to get understanding of RunCommad on VM : https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command-managed . You can Install the Az Module on PowerShell https://learn.microsoft.com/en-us/powershell/azure/install-azps-macos?view=azps-10.3.0 and you are good to go! Please "Accept as Answer" if this helps .

    0 comments No comments

  2. Deepanshu katara 17,960 Reputation points MVP Moderator
    2023-09-11T04:40:50.4633333+00:00


    Ram Kumar Cheekoti
    , can you please accept my above script answer , I believe it has helped you in your issue, Thanks

    0 comments No comments

Your answer

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