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 (52)

  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!

    1. @Matt, great question. In that case, you don’t really need the script – you could just enable it via the cmdlet here. You will still need the latest AzureRM.Insights which requires you to update your AzureRM module set so see the instructions at the top of the post. But below you could use something similar with a one liner to turn it on (you’ll need the resourceID of your Workspace as well as your Azure Resource (Database for example) which you can find both of those by just browsing to the resource in Azure, and taking everything from “/Subscriptions/….” all the way through till the end of the actual resource name in Azure.

      PS C:\> Set-AzureRmDiagnosticSetting -ResourceId “/subscriptions//resourceGroups//providers/Microsoft.Sql/servers//databases/” -WorkspaceId “” -Enabled $True

      1. Matt says:

        @Jim, thanks so much for your help I got it going and it works great.

  9. Isa says:

    Hi Jim,
    I am having trouble running the script. Which version of Powershell you using?
    Thanks,
    Isa

    1. @ISA – what are the errors you are getting? You need to be running with the latest AzureRM modules as a first requirement.

      My environment looks like this

      PS C:\Users\jbritt> $PSVersionTable
      Name Value
      —- —–
      PSVersion 5.1.15040.0
      PSEdition Desktop
      PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
      BuildVersion 10.0.15040.0
      CLRVersion 4.0.30319.42000
      WSManStackVersion 3.0
      PSRemotingProtocolVersion 2.3
      SerializationVersion 1.1.0.1

      PS C:\Users\jbritt> $host
      Name : ConsoleHost
      Version : 5.1.15040.0
      InstanceId : b0720b3b-ea02-478f-82f3-7075ca94de78
      UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
      CurrentCulture : en-US
      CurrentUICulture : en-US
      PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
      DebuggerEnabled : True
      IsRunspacePushed : False
      Runspace : System.Management.Automation.Runspaces.LocalRunspace

  10. Soma Jagadeesh says:

    Hello ,

    I am having below error while trying to execute the scripts tried with installing all pre-requisites
    Please enter a selection from 1 to 7: 1
    Selecting Azure Subscription: f5599661-388e-4489-877b-f1e918563604 …
    Get-AzureRmOperationalInsightsWorkspace : Run Login-AzureRmAccount to login.
    At C:\Users\sojaga\desktop\temp\Enable-AzureRMDiagnostics.ps1:285 char:43
    + … es = Add-IndexNumberToArray (Get-AzureRmOperationalInsightsWorkspace)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-AzureRmOperationalInsightsWorkspace], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.OperationalInsights.GetAzureOperationalInsightsWorkspaceCommand

    Generating a list of workspaces from Azure Subscription Selected…
    No OMS workspaces available in selected subscription f5599661-388e-4489-877b-f1e918563604
    At C:\Users\sojaga\desktop\temp\Enable-AzureRMDiagnostics.ps1:314 char:9
    + Throw “No OMS workspaces available in selected subscription $ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (No OMS workspac…7b-f1e918563604:String) [], RuntimeException
    + FullyQualifiedErrorId : No OMS workspaces available in selected subscription f5599661-388e-4489-877b-f1e918563604

    1. @Soma Jagadeesh – the error you are seeing indicates that your AzureRM modules are unfortunately still out of sync. It could be that you have more than one directory providing your module path for AzureRM. If you do an

      install-module -name AzureRM -Force

      what happens?

      1. IAMPSJAG says:

        Jim,

        Post running the command below is the error we are receiving:
        PS C:\windows\system32> install-module -name AzureRM -Force
        PackageManagement\Install-Package : Cannot process argument transformation on parameter ‘InstalledModuleInfo’. Cannot convert the
        “System.Object[]” value of type “System.Object[]” to type “System.Management.Automation.PSModuleInfo”.
        At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772 char:21
        + … $null = PackageManagement\Install-Package @PSBoundParameters
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidData: (Microsoft.Power….InstallPackage:InstallPackage) [Install-Package], Exception
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Validate-ModuleAuthenticodeSignature,Microsoft.PowerShell.PackageManagement.Cmd
        lets.InstallPackage

        PS C:\windows\system32>

        1. @AMPSJAG can you attempt to reinstall your modules leveraging WebPI. It seems that you may have a mix of older and newer modules spread across two different paths. This can happen if you at one time used WebPI to install the Azure SDK and then used PS Get to install ARM Modules later. The conflict arises when you try to update with PS Get.

  11. Akihiro Kaneko says:

    Hello Jim,
    I completed by step 7, but i can’t see any metrics of web App & SQL Datbase.
    Windows VM & Linux VM can get metrics on Log Analytics.
    Thanks.
    Akihiro

    1. @Akihiro

      It is possible you aren’t seeing these where you think you should. The metrics are captured and sent to this type in Log Analytics
      Type=AzureMetrics

      Details on Azure SQL are below for sending (and viewing) w/in Log Analytics. There is also a WebApps solution in the Azure Marketplace that you can leverage that will visualize the WebApps metrics once you have configured it with this script in this post.

      https://blogs.technet.microsoft.com/msoms/2017/02/21/azure-sql-analytics-solution-public-preview/
      https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-azure-sql

  12. David White says:

    Is there any other step not mentioned here that might trip the script up? I have one account and one OMS instance. Wen I puck server farm or database or web sites I get a message similar to:
    An error occurred setting diagnostics on bakroundnoise of type [Microsoft.Web/serverFarms]
    That’s it. No other clues other than that. Is there a more detailed log somewhere?

    1. @David, please check the version of AzureRM.Insights. The error you are seeing indicates potentially that your version doesn’t have the workspaceID option available. I believe this parameter became available in version 2.6 of the AzureRM.Insights module.

      get-module -name AzureRM.Insights | FL

      The above command should give you the information to see what version you have. Likely it is pre-2.6. If so, you need to update your AzureRM Module set by following the process in this blog post at the top.

  13. Larry says:

    Hello, Jim,
    We’re using Elastic Pools. Although it’s not necessary to refresh the setup for the pools, I assume every new Azure SQL Database you might add to your subscription must be set up anew?

    Would you recommend using this script to maintain diagnostics configuration in an Automation context? Or would you rather recommend a custom script to be run for each new database added at the time of creation?

    Thanks.

    1. @Larry, you are correct in assuming that new elastic pools will not be picked up. An option could be to run this as an automation runbook on some schedule to pick up new elastic pools and dbs as they are created. You can leverage the -force parameter along with the required parameters to make the script run silent. This should solve the scenario for you.

  14. James says:

    Hi Jim,

    Love the script, thank you for this. Just a quick question, we have currently around 250 Azure SQL databases across two subscriptions, with roughly 4 added each week. Is there a way to ensure any newly created databases have this option enabled by default?

    Secondly, although we have two subscriptions, our OMS workspace is in one subscription only, how do we enable the metrics on the subscription where the OMS workspace is not present?

    Finally, we too would need a disable option, it has my vote if you are considering this.

    Kind regards

    James

    1. @James, I think we covered over email 🙂 but to answer here on the forum.

      you can provide a command line parameter during the execution of the script –WSID and provide the resourceID of the workspace to use that workspace by default. Then you can run the script as you normally would, select that subscription that has the resources you want to send to Log Analytics in the other subscription but since you have the parameter specified during execution, it will take that as the parameter to configure the resource. If you do a “get-help .\scriptname –examples” with the script it will show you one of those examples. Also in the blog post for the solution, there is an option to do an “advanced scenario” and that is exactly what that is showing, leveraging WSID parameter to have more than one subscription and resources supported a single workspace

      I added the option of –force to allow you to run this silently. It will check that you have provided all the parameters needed for a silent execution but that should allow you to use this in an Azure Automation runbook for example – run it on a schedule at whatever frequency you are comfortable with, and it can pick up the deltas on your behalf as they enter your environment. You can also leverage ARM templates and should be able to set the settings for enabling diagnostics during the deployment of a resource Via ARM. The future goal is to leverage Azure Policy to allow you to configure your environment to enforce things such as management and monitoring but that is roadmap and not available yet.

      To disable the metrics you can provide the parameter of (-Enabled $False -TimeGrain “PT1M”) as you mentioned on the set-AzurermDiagnosticSettings cmdline and that will turn it off. Currently I don’t have the script providing that option but it is fairly easy to add it in there if you decide to. Additionally, we’ll be introducing with this cmdlet the option to disable on storage account, eventhub, metrics, etc. but that won’t be available for a month or so. For now when you disable on a resource, you disable all options (eventhub, storage account, log analytics).

      1. Script has been updated: https://www.powershellgallery.com/packages/Enable-AzureRMDiagnostics/ to now support Logs as well as support the option of disabling specific logs as well as metrics on resources if needed. Great for testing and debugging or fixing accidental configurations :). See get-help .\script -examples for more information. See get-help .\script -detailed for full details of examples, updated synopsis, etc.

  15. Dave says:

    When I run the script I receive an exception. I’ve tried updating AzureRM modules several times, including forcing an upgrade to versions 2.5.0 and 2.6.0, all with the same error each time. Thinking it was a permissions issue, I logged in as the account owner. Here’s the exception:

    Set-AzureRmDiagnosticSetting : Exception of type ‘Microsoft.Rest.Azure.CloudException’ was thrown.
    At C:\temp\Enable-AzureRMDiagnostics.ps1:302 char:41
    + … $Diag = Set-AzureRmDiagnosticSetting -WorkspaceId $WSID -Resource …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Set-AzureRmDiagnosticSetting], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

    I am passing in a WSID to enable logging across subscriptions. The list of subscriptions appears, I select a subscription (we have 4), a list of resource types appears (there are 3). I choose the first one (service bus) and then ask it to enable operational logs. The script reports each service bus is updated but this exception is thrown for each service bus and no data appears in OMS. I have received the error several times and have tried several different selections, it’s not just a service bus resource type issue.

    Can you point me in the right direction of what might be causing the exception?

    1. @Dave – a couple of things you can check.
      1. Can you ensure that under your subscription (for the resources as well as the OMS workspace) that under Resource providers, that microsoft.insights is registered?
      2. Can you validate that set-azurermdiagnosticsetting will take -workspaceID?
      3. Is the workspaceID being sent as a parameter and if so is it being sent as a resourceID (not the WorkspaceID)?

      Example
      set-azurermdiagnosticsetting -ResourceID -workspaceID -Enabled $True

      set-azurermdiagnosticsetting -ResourceID “/subscriptions//ResourceLocation” -workspaceID “/subscriptions/fd2323a9-2324-4d2a-90f6-7e6c2fe03512/resourceGroups/OI-EAST-USE/providers/Microsoft.OperationalInsights/workspaces/OMSWS”

      Email me direct if you want as well: jbritt@microsoft.com and we can troubleshoot over email or skype. :).

      1. Dave says:

        @Jim – Thank you for the quickly reply. I checked all of our subscriptions and under “Resource providers” Microsoft.Insights is listed as being registered. I did not attempt to re-register them.

        This issue might be with Set-AzureRmDiagnosticSetting – when I run that command I get an invalid api-version. When I try to run install-module AzureRM.Insights -Force it tells me that version 2.8.0 is installed and in use. Here’s the error text below:

        PS C:\temp> set-azurermdiagnosticsetting -ResourceID $RIC -workspaceID $WSID
        set-azurermdiagnosticsetting : The api-version ‘2015-07-01’ is invalid. The supported versions are
        ‘2017-03-01,2016-09-01,2016-07-01,2016-06-01,2016-02-01,2015-11-01,2015-01-01,2014-04-01-preview,2014-04-01,2014-01-01,2013-03-01,2014-02-26,2014-04’.
        At line:1 char:1
        + set-azurermdiagnosticsetting -ResourceID $RIC -workspaceID $WSID
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : CloseError: (:) [Set-AzureRmDiagnosticSetting], CloudException
        + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

        The commands I’m running per item 2 you provided is:

        $WSID = “/subscriptions/<>/resourcegroups/oms/providers/microsoft.operationalinsights/workspaces/CompanyNameOms”
        $RIC = “/subscriptions/<
        >/Microsoft.ServiceBus/namespaces”

        set-azurermdiagnosticsetting -ResourceID $RIC -workspaceID $WSID

        In case I didn’t mention it, I tried running the commands in the article on a separate computer and received the same errors.

        1. @Dave, Sorry for my delayed response. I’m wondering if there is an issue specific to Service Bus. Have you tried this with any other Azure Resources? I’m going to do some testing to see if I can figure out what may be going on. I’ll report back. You can email me directly as well if you are in a jam. Happy to help over email to speed things up. I don’t get notified on updates to this unfortunately :).

        2. @Dave, Updating my previous comment. So I did create a Service Bus within my subscription, validated that both metrics and logs can be enabled on that resource. BTW, if you just hit enter on the categories prompt, it will enable metrics if you say yes to the update question. You can also use now EnableMetrics and EnableLogs on the command line as parameter switches to enabling each or only one on a resource if it supported.

          Since this did in fact work for my scenario – wondering if either the workspace is in a different tenant than the resources or if you don’t have contributor access to both the Workspace and the resource you are trying to configure? You need to have rights to not only see it but also update it in order for you to be able to make the linkage between the two.

          Since your cmdlets were saying that 2.80 was installed but in use, if you can figure out where your AzureRM.Insights module is actually located – you could update it again to make sure no corruption but I know that you stated that you ran this from another system.

          Import-Module -name AzureRM.Insights
          get-command -name Set-AzureRmDiagnosticSetting|fl

          The above will give you path information
          Path :
          AssemblyInfo :
          DLL : C:\Program Files\WindowsPowerShell\Modules\AzureRM.Insights\2.7.0\Microsoft.Azure.Commands.Insights.dll
          HelpFile : Microsoft.Azure.Commands.Insights.dll-Help.xml
          ParameterSets : {-ResourceId [-StorageAccountId ] [-ServiceBusRuleId ] [-Enabled ]
          [-Categories ] [-Timegrains ] [-RetentionEnabled ] [-WorkspaceId
          ] [-RetentionInDays ] []}
          ImplementingType : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

          If your path is like the above, then you likely installed it via PS Get and can re-install all modules by doing the following
          install-module -name AzureRM -force

          Otherwise, if your path is containing “SDK” it was likely installed by Web Platform Installer and therefore I’d recommend going to start / search for Web Platform Installer / then once launched, search for Microsoft Azure PowerShell and re-install from there. In either case, all PowerShell consoles and ISE environments need to be closed. WebPI will close them for you 🙂 but PowerShell GET will not so just make sure you have all consoles closed before starting the above process – for PowerShell Get just launch a fresh admin console and you should be good to go with all other consoles terminated. I suspect rights still but it would be good to eliminate a partial configuration of ARM modules.

  16. Anusha says:

    Hi, Thanks for detailed information. Is there anyway to select a specific database to onboard instead of all DBs in selected subscription?

    1. @Anusha, I just enabled this option with the latest version of the script so good timing :). You can see all updated examples by doing a “Get-Help .\scriptname -examples” to display new options. For the scenario you are attempting to do, it would be similar to the below example

      ————————– EXAMPLE 8 ————————–

      PS C:\>.\Enable-AzureRMDiagnostics.ps1 -SubscriptionId “fd2323a9-2324-4d2a-90f6-7e6c2fe03512” -ResourceName
      “MyResource” -EnableMetrics -ResourceType “Microsoft.EventHub/namespaces”

      [Enable] all metrics for all specified resource named of a resourceType within a targeted subscription
      Note if resourcetype is left off – resource must support requested operation or error will be thrown

      I hope this helps – please let me know! And thanks for leveraging the script and providing feedback!

      1. Anusha says:

        Thanks Jim for quick response. I tried using ResourceGroupName parameter(I have only one DB under this RG). It ran successfully but I am still not able to see data in OMS workspace. it shows as “Performing Assessment” from more than 3 hours now.

        Please enter a selection from 1 to 6: 5
        You Selected Workspace: xxxx-xxxx-MIGRATION-PPE
        Gathering a list of monitorable Resource Types from Azure Subscription ID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx…

        Microsoft.Sql/servers/databases
        This operation will update 1 Microsoft.Sql/servers/databases resources in your subscription. Continue?
        [Y] Yes [N] No [S] Suspend [?] Help (default is “Y”): y
        Configuring 1 [Microsoft.Sql/servers/databases] resources in your subscription.
        Processing resources. Please wait….
        Metrics gathering for new resource xxxxxxxxxxxxxx/OMSMigrationTest was set to enabled
        Complete

        Under OMS portal:
        Azure SQL Analytics (Preview)
        Performing Assessment
        Ensure that you have run the script below to onboard your Azure PaaS Resources to send data to OMS. https://aka.ms/azurepaasonboarding

        1. @Anusha, hmm…can you try to see if the data is showing up directly in Log Search?

          type=AzureMetrics

          You should see some data in there if it is populating. Also, attempt to close all browsers and re-open in case this is an odd caching issue though this should not be the case.

  17. Peter Hancock says:

    Can we do this within an ARM template?

    1. @Peter – yes. Please take a look at the following url: https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-azure-storage and go to the section titled “Enable diagnostics with Resource Manager templates”

  18. Note you need to install psget to get this example to work from http://psget.net/. Took me a while to figure this out.

  19. Mayzing says:

    Hi Jim,

    It is failing for me using my Visual Subscription. From OMS, no logs are being collected. I have the latest modules.

    Authenticating to Azure…
    Selecting Azure Subscription: …
    Configuring 1 [Microsoft.Sql/servers/databases] resources in your subscription.
    Processing resources. Please wait….
    Set-AzureRmDiagnosticSetting : Exception type: CloudException, Message: Please register the subscription ” with Microsoft.Insights., Code: Null, Status
    code:Conflict, Reason phrase: Conflict
    At C:\temp\Enable-AzureRMDiagnostics.ps1:324 char:33
    + … $Diag = Set-AzureRmDiagnosticSetting -WorkspaceId $WSID -Resource …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Set-AzureRmDiagnosticSetting], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

    Metrics gathering for new resource chasqlsrvlozanom/AdventureWorksLT was set to enabled
    Complete

    1. @Mayzing, thanks for your interest, trying the script and providing feedback. The specific error you are seeing is actually related to not having a resource provider properly registered within your Azure Subscription. Please register this resource provider in both the subscription that you are attempting to monitor resources in as well as the subscription where your OMS workspace is located (if they are not the same subscription).

      Details on this are documented here: https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-azure-storage in the section entitled “Troubleshoot Azure Diagnostics”. Additionally, please download the very latest script as there were some recent changes to the Azure RM CMDLETS that would prevent this script from executing properly once you resolved the resource provider issue above. Updated script located here: https://www.powershellgallery.com/packages/Enable-AzureRMDiagnostics/2.51/DisplayScript

  20. G. Pradeep Kumar Reddy says:

    Hi,
    I am trying to enable metrics and diagnostics for single database instead of all my databases, for that I used below cmdlet

    Set-AzureRmDiagnosticSetting -ResourceId “/subscriptions/XXXXXX-XXX-XXX-XXX/resourceGroups/YYYYY/providers/Microsoft.Sql/servers/YYYY/databases/” -WorkspaceId “XXXXXX-XXX-XXX-XXX” -Enabled $True

    But when I run the above cmdlet in PowerShell ISE with Administrator mode, I got the below error even I install the Azure RM Modules.

    Set-AzureRmDiagnosticSetting : Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:NotFound, Reason
    phrase: Not Found
    At line:1 char:1
    + Set-AzureRmDiagnosticSetting -ResourceId “/subscriptions/13231232332 …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Set-AzureRmDiagnosticSetting], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

    Thanks!

    1. @G. Pradeep Kumar Reddy – for workspaceID the cmdlet is actually expecting a resourceID of the OMS workspace. So similar to your resourceID for your resource you are configuring.

      Note the script can support this scenario as well with the example pasted below from the script.

      ————————– EXAMPLE 8 ————————–

      PS C:\>.\Enable-AzureRMDiagnostics.ps1 -SubscriptionId “fd2323a9-2324-4d2a-90f6-7e6c2fe03512” -ResourceName
      “MyResource” -EnableMetrics -ResourceType “Microsoft.EventHub/namespaces”

      [Enable] all metrics for all specified resource named of a resourceType within a targeted subscription
      Note if resourcetype is left off – resource must support requested operation or error will be thrown

      1. G. Pradeep Kumar Reddy says:

        @Jim, I used the below cmdlet for configuring the log analytics to single database.

        .\Enable-AzureRMDiagnostics.ps1 -SubscriptionId “5XXXXXX” -ResourceName “Research_db” -EnableMetrics -ResourceType “Microsoft.Sql/servers/databases”

        when i run the above cmdlet in Power shell, I got the error like below.
        No diagnostic capable resources of type Microsoft.Sql/servers/databases available in selected subscription
        XXXXXXXXXXXXXXXXXX
        At E:\User\PowershellScripts\Enable-AzureRMDiagnostics.ps1:610 char:9
        + Throw “No diagnostic capable resources of type $ResourceType …
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : OperationStopped: (No diagnostic c…c7-ba2ae87ffcda:String) [], RuntimeException
        + FullyQualifiedErrorId : No diagnostic capable resources of type Microsoft.Sql/servers/databases available in selected subscrip
        tion XXXXXXXXXXX

        Any idea?

        Thanks.
        Pradeep

        1. @G. Pradeep Kumar Reddy to configure it in that way for SQL DBs you need to provide the name a bit differently. Something like the below indicating the DB and server it is located on. The script does that for you behind the scenes but if you supply the name, it then gets the resourceID for that name. Hope that makes sense.

          “contactmanagersub2server/ContactManagerWebAppSub2_db”

Skip to main content