Enable Azure resource metrics logging using PowerShell


Summary: Use a script to enable metrics logging for PaaS resources.

Azure Platform as a Service (PaaS) resources, like Azure SQL and Web Sites (Web Apps), can emit performance metrics data natively to OMS. This script allows users to enable metrics logging for PaaS resources at a the level of a subscription or resource group. Today, there is no way to enable metrics logging for PaaS resources through the UI. Therefore, customers need to use a PowerShell script. This native metrics logging capability and OMS monitoring enable customers to monitor Azure Resources at scale. For example, you can now use OMS to monitor hundreds of thousands of SQL Azure databases in one OMS workspace.

Prerequisites

Make sure that you have the following AzureRM modules installed on your workstation before you begin:

  • AzureRM.Insights
  • AzureRM.OperationalInsights
  • AzureRM.Resources
  • AzureRM.profile

Note: We recommend that all your Azure Resource Manager modules are the same version to ensure compatibility when you run Azure Resource Manager commands from PowerShell.

To install the latest set of AzureRM modules on your workstation:

PS C:\> install-module -Name AzureRM -Force

  1. Open PowerShell in Administrator mode: Run As Administrator.
  2. To save the Enable-AzureRMDiagnostics.ps1 script file locally, run the following command and provide a path to store the script.

PS C:\> save-script -Name Enable-AzureRMDiagnostics -Path "C:\users\<username>\desktop\temp"

  1. Go to the folder where you saved the script, and execute Enable-AzureRMDiagnostics.ps1.

PS C:\users\<username>\Desktop\temp> .\Enable-AzureRMDiagnostics.ps1

Notes:

  • If you want to enable metrics logging for a specific Resource Group instead of the entire subscription, you can use these advanced steps.
  • If you have not already authenticated to Azure, this script will prompt you for your credentials.
  • For a synopsis with examples for the script, just type get-help <PathName to Set-AzureDiagnosticsSetting.ps1> -Examples.
  1. Select the Azure Subscription that has the Azure resources that you want to monitor.

Select the Azure Subscription that has the Azure resources that you want to monitor

  1. Select the OMS Log Analytics Workspace that you want to have for this data set.

Select the OMS Log Analytics Workspace that you want to have for this data set

  1. Select the Azure Resource Type that you would like to enable. For example, if you type 5, you enable the metrics data collection for SQL Azure Databases.

Select the Azure Resource Type that you want to enable

  1. Type Y to enable metrics logging for all databases in that subscription.

The PowerShell script will run against each database and enable the metrics collection for those resources. After it’s finished, you will see a Complete message.

After completion, you will start to see metrics data in OMS Log Search as shown in the following screenshot. The data from the Azure PaaS resource can be found under Type=AzureMetrics. The new Azure SQL and Web App solutions will use this data.

Metrics data in OMS Log Search

Advanced scenarios

Enable metrics for a specific Resource Group

The following steps enable you to specify the resource group for which you would like to enable metrics logging.

Run the following command:

PS C:\> .\Enable-AzureRMDiagnostics.ps1-ResourceGroup "anugup"

The rest of the prompt will remain the same as previously shown in step 5.

You can find the resource group name in the Azure portal as shown in the following screenshot.

Resource group name in the Azure portal

Update a resource to send data to another workspace

If you have a resource type that is already sending metrics to a workspace, but you want to change it to another workspace, you can use the -Update switch.

Run the following command:

PS C:\> .\Enable-AzureRMDiagnostics.ps1-Update

-or-

PS C:\> .\Enable-AzureRMDiagnostics.ps1-Update -ResourceGroupName “Leyla”

The rest of the prompt will remain the same as previously shown in step 5.

Examples for the PowerShell script

For a synopsis with examples for the script, you can also just type GET-HELP .\Enable-AzureRMDiagnostics.ps1 -Examples.

Result of GET-HELP .\Enable-AzureRMDiagnostics.ps1 -Examples

The complete script is available in the PowerShell Gallery.

Jim Britt
Senior Program Manager

Comments (17)

  1. Florent says:

    Hello,
    I’ve the following error when I execute the script, at step 5:

    Add-IndexNumberToArray : Cannot bind argument to parameter ‘array’ because it is an empty collection.

    Any idea?
    Thanks.
    Florent

    1. @Florent, have you downloaded the latest script version? There was a bug in the initial version where the array was not instantiated properly when a single value was returned (aka created a string).  What version are you currently leveraging (should be in the header of the script).

      1. Florent says:

        @Jim, I deleted the script and downloaded it again and now, it works 🙂 Thanks.

  2. pini Krisher says:

    Thanks
    1. can i change work space?
    2. what is the interval for the Metric collection?

    pini

    1. @Pini – you can use the -update switch and provide a new WSID as a parameter or simply use -update with the menu process to select a new workspace. I believe the interval is now 1 minute for metrics.

  3. Funzel says:

    mhh I get no OMS workspaces available after selecting subscription. Any requirements on the workspace like pricing tier ?

    1. Funzel says:

      solved, works for me only with PS ISE

      1. @Funzel this is more than likely an issue with PowerShell module path and what modules are getting picked up and leveraged during execution. I may add some additional checks in the script to ensure that all modules are available and the same version.  Something to check here.

        in your failing PowerShell console do the following:
        Get-Module -name AzureRM.Profile | fl *

        Do the same in your working ISE environment and see the difference. You are likely running from different versions of AzureRM modules. WebPI installs to the SDK directory for Azure on your system into a ResourceManager folder. PowerShell Get (install-module) installs into c:\windows\system32\windowsPowerShell. You may even have other folders. The above command will expose the folder and the version to allow you to determine if you are running with the latest and greatest.

        AzureRM.Profie, AzureRM.Resources, AzureRM.Insights, AzureRM.OperationalInsights all need to be at the same version and be at least version 2.5 to support sending data to Log Analytics from Azure Diagnostics. Could you take a look at your PowerShell environment between your PowerShell console and ISE and see if there is something there regarding different modules being leveraged from different paths?

  4. Jean POIZAT says:

    Hello, it did not work for me with only one resource to update.
    it seems there is missing part at the end to deal with this scenario…

    here is the part of code that worked for me :
    if ( $DiagnosticCapable.’#’.Count -eq 1)
    {
    $ResourceTypeToProcess = 1
    $ResourceType = $DiagnosticCapable.resourcetype
    } else {
    while($ResourceTypeToProcess -gt $DiagnosticCapable.’#’.Count -or $ResourceTypeToProcess -lt 1 -or $ResourceTypeToProcess.GetType().Name -ne “int32” -and $Force -ne $True)
    {
    Write-Host “In following table are the resources that you can send diagnostics to Log Analytics”
    $DiagnosticCapable | select “#”, ResourceType |ft
    [int]$ResourceTypeToProcess = Read-Host “Please select a number from 1 – $($DiagnosticCapable.count) to enable”
    }
    $ResourceType = $DiagnosticCapable.resourcetype[$ResourceTypeToProcess -1]
    }

    1. @Jean, thanks for the feedback. This bug should have been fixed with the latest version of the script. Let me investigate and update appropriately thank you for the feedback.

  5. Brett says:

    Can we send data from a customer in a different Azure tenant?
    More info here – serverfault.com/questions/826786/

    1. @Brett – as long as the Azure AD ID you are using has access to both resources, I believe you can. Currently I’m only aware of this working within a tenant however there shouldn’t be any reason why this wouldn’t work.  I have not tried this however as internally I’m dealing only with the Microsoft tenant. Curious your results here. You would obviously need to provide the WSID parameter since this will be not only in a separate subscription, but also a separate tenant.

  6. Usman says:

    Now how to disable it?

    1. @Usman, the script executes iteratively against resources provided and uses the -Enable $True. You can update the script (or take a portion of the script) to reverse this to -Enable $False to turn off diagnostics going to LA. The script currently doesn’t provide this option but I’m looking into potentially adding it.

  7. murthy says:

    I am getting the below error while running the Enable-AzureRMDiagnostics.ps1. I see the subscription has few OMS workspaces.

    Please enter a selection from 1 to 124: 121
    Selecting Azure Subscription: d1xxxx-b11e-41eee-92eyyy-71xxxxxxx …
    Get-AzureRmOperationalInsightsWorkspace : Run Login-AzureRmAccount to login.
    At C:\Program Files\WindowsPowerShell\Scripts\Enable-AzureRMDiagnostics.ps1:285 char:43
    + … es = Add-IndexNumberToArray (Get-AzureRmOperationalInsightsWorkspace)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-AzureRmOperationalInsightsWorkspace], PSInvalidOperationExcep
    tion
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.OperationalInsights.GetAzureOperationalInsight
    sWorkspaceCommand

    Generating a list of workspaces from Azure Subscription Selected…
    No OMS workspaces available in selected subscription d1be0ef8-b110-417e-992e-71a61d0e06cf
    At C:\Program Files\WindowsPowerShell\Scripts\Enable-AzureRMDiagnostics.ps1:314 char:9
    + Throw “No OMS workspaces available in selected subscription $ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (No OMS workspac…2e-71a61d0e06cf:String) [], RuntimeException
    + FullyQualifiedErrorId : No OMS workspaces available in selected subscription d1be0ef8-b110-417e-992e-71a61d0e06cf

    1. @Murthy From the looks of the error, you need to update your AzureRM modules. See the pre-requisites at the top of the post.

      Prerequisites
      Make sure that you have the following AzureRM modules installed on your workstation before you begin:

      AzureRM.Insights
      AzureRM.OperationalInsights
      AzureRM.Resources
      AzureRM.profile

      More than likely you have a different version of AzureRM.Profile than you have of AzureRM.OperationalInsights. All of the above modules need to be the same version. The best way to ensure you have the latest is to do as described in the post above as well.

      From an elevated PowerShell console (closing all other consoles) run the below

      install-module -Name AzureRM -Force

      The above will ensure you have the latest. If you run into issues installing the modules, more than likely you are dealing with modules that are located in various locations on your system.

      Please look at the specifics on my response to the question @Funzel posted in the comments above for more information.

  8. Matt says:

    Thanks for this blog.

    Is there a way to pinpoint a single database that I want to enable instead of all my databases?

    I see that you can enable a resource type, but can I get more specific about which database to enable?

    Thanks!

Skip to main content