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