Installing Enhanced Monitoring Agent for Multiple SAP workloads in Azure - Code Sample

@20aman    Jan 14, 2022

In the previous blog post, we saw how to install the enhanced monitoring extension for SAP workloads in Azure. We looked at various caveats linked to the installation and pre-requisites etc. You can view that post here: Installing Enhanced Monitoring Agent for SAP workloads in Azure. In this post, I want to share the script sample for installing the extension for multiple VMs in a single go.

Latest Sample Location on GitHub

You can find the latest script sample in my GitHub repository here: https://github.com/HarvestingClouds/PowerShellSamples/tree/master/Scripts/Install-SAPMonitoringExtensionInLoop

The raw version can be found directly here: https://raw.githubusercontent.com/HarvestingClouds/PowerShellSamples/master/Scripts/Install-SAPMonitoringExtensionInLoop/Install-SAPMonitoringExtensionInLoop.ps1

The script sample and details

Just like for a single VM, the script uses the "Set-AzVMAEMExtension" cmdlet to install the extension with the "InstallNewExtension" switch. In this scenario, we pull all VMs from a single or multiple resource groups. The resource group is selected either based on the wildcard match or you can specify a particular resource group name as well. Each VM is then checked if this is Linux or Windows. Make sure that all the VMs have SAP workloads before proceeding with the installation. You can also uninstall the extension if it is not aplicable on any of the VM later.

The script sample is also provided below for your easy reference.

<#  
 .NOTES
    ==============================================================================================
    File:       Install-MonitoringExtensionInLoop.ps1

    Purpose:    To Install Monitoring Extension In Loop

    Version:    1.0.0.0 

    Author:     Aman Sharma
    ==============================================================================================
 .SYNOPSIS
    Installs monitoring extension in loop on Linux VMs

 .DESCRIPTION
    This script is used to Installs monitoring extension in loop on Linux VMs

 .EXAMPLE
    C:\PS>  .\Install-MonitoringExtensionInLoop.ps1

    Description
    -----------
    This command executes the script with default parameters.

 .INPUTS
    None.

 .OUTPUTS
    None.

 .LINK
    https://docs.microsoft.com/en-us/azure/virtual-machines/workloads/sap/deployment-guide#2ad55a0d-9937-4943-9dd2-69bc2b5d3de0
#>

#Inputs
$subscriptionName = "you-subscription-name"

#Adding Azure Account and Subscription
$env = Get-AzEnvironment -Name "AzureCloud"
Connect-AzAccount -Environment $env
Set-AzContext -SubscriptionName $subscriptionName

#Selecting all RGs that begins with the text. Notice the wildcard in the name
#TODO: Update this to a specific resource group or a similar query with wildcards
$allSapRGs = Get-AzResourceGroup -Name "RG-IT-*"

foreach($currentRG in $allSapRGs)
{
    #Fetch all resources in the RG
    $currentRGName = $currentRG.ResourceGroupName
    $VMs = Get-AzVM -ResourceGroupName $currentRGName

    #Iterating on the VMs
    foreach ($vm in $VMs) 
    {
        $VMName = $vm.name
        $osType = $vm.StorageProfile.OsDisk.OsType
        Write-Host "Working on VM: $VMName"

        if ($osType -eq "Linux") {
            Write-Host "VM $VMName is a Linux VM. Proceeding with the installation."
            try {
                Set-AzVMAEMExtension -ResourceGroupName $currentRGName -VMName $VMName -InstallNewExtension

                Write-Host -ForegroundColor Green "Installed the extension on the VM $VMName"
            }
            catch {
                Write-Host -ForegroundColor Red "Error while installing extension."
                $Error[0]
                Write-Host -ForegroundColor Red "Error occured at:"
                $Error[0].InvocationInfo.PositionMessage
            }
        }
        else {
            Write-Host "VM $VMName is not a Linux VM"
        }

    }
}





Comments powered by Disqus