Logging Azure Application Insights telemetry data from PowerShell

Application Insights (AI) is the application performance management (APM) and logging platform for Microsoft Azure. They provide a client instrumentation library for several popular platforms/languages– but there isn’t any official module for PowerShell. In this post I share some new functions that demonstrate how to log telemetry data to AI from PowerShell.

Design Approach

Most of the other examples online for logging AI telemetry data within PowerShell will have you have download the Application Insights instrumentation library for .NET, import the library, construct all the objects, and call the Track() methods.

The problem with this approach is that it relies on you downloading and packaging the .NET library with your PowerShell functions or module. This is perfectly fine if you need the full functionality of the instrumentation library. However if you only need to log some of the more simple telemetry like traces and events– then a lightweight and cross platform friendly option is to just call the Application Insights REST API directly from PowerShell and bypass client instrumentation libraries.

Using a web trace tool like fiddler we can watch the traffic that the AI instrumentation library is sending and then write helper functions that mimic that traffic.

Source Code

I wrote 3 helper functions for the most common Track operations I use on a regular basis. You can find the source code for these functions in my AzurePowerShellUtilityFunctions module on Github. Here are the examples and what they map to in the instrumentation library:

FunctionClient Lib FunctionSource Link
Send-AppInsightsTraceTelemetry TrackTrace()Link
Send-AppInsightsEventTelemetry TrackEvent()Link
Send-AppInsightsExceptionTelemetryTrackException()Link

Example Usage

Download the module source code from Github, import it into your current session, then just call the function.

# assumes your module was downloaded to C:\source
Import-Module C:\Source\AzurePowerShellUtilityFunctions\AzurePowerShellUtilityFunctions.psd1

# plug in your AI instrumentation key, trace message, and severity.
Send-AppInsightsTraceTelemetry -InstrumentationKey '<guid>' -Message 'This is an information logging message.' -Severity Information 

You can also optionally specify custom properties that get logged with any of these operations. Here is an example for a custom event:

# log a custom event with extra custom properties
Send-AppInsightsEventTelemetry -InstrumentationKey '<guid>' -EventName 'MyApplicationEvent1' -CustomProperties @{ 'CustomProperty1'='abc'; 'CustomProperty2'='xyz' }

4 thoughts on “Logging Azure Application Insights telemetry data from PowerShell

  1. josy1024 April 18, 2021 / 9:20 pm

    are there any prerequisites for use with ” -CustomProperties”?

    Like

    • keithbabinec April 18, 2021 / 11:13 pm

      No prerequisites are required to use the CustomProperties argument. To use this argument just make sure your are sending a hashtable object with key-value pairs in it, like the example above.

      Like

  2. Jay December 2, 2021 / 11:29 pm

    Hi, how does the authentication done here? do you need to setup AZ cli or put your App Insights API key somewhere?

    Like

    • keithbabinec December 4, 2021 / 1:47 pm

      @Jay — all API calls that send telemetry to the ingestion point are unauthenticated. The reason for this is that App Insights is frequently used by public clients/apps, so you may need to send telemetry from untrusted locations without logging in. The ‘instrumentation key’ is not an authentication secret, but more of just a public ID to reference which bucket the telemetry should land in. It is safe to store the instrumentation key directly in the client config.

      Like

Leave a comment