Using Azure Application Insights In PowerShell

Last week while working with Azure PowerShell, I came across this specific need to build a solid logging framework wherein all the information would be dumped from the PowerShell and could be used to build reporting features. Having worked with Azure for quite some time, my quick response was – hey, why don’t we use Azure applications insights here? It has already got all the base framework needed and Azure portal gives flexibility to show the logged information effectively. So yeah – sounds that’s the right candidate!

But wait, having said that we will be logging information from PowerShell and not from any .NET based application so how do we achieve it? Do I need to search if Azure Application Insights has any public endpoints which I need to consume from my PowerShell script?

After few hours of searching, I got the solution and believe me it’s quite an easy one and ‘readymade-ish’.

Since Application insights already provides managed APIs to work with it using any .NET based application and Windows PowerShell is based on Windows platform too so we can directly import any managed assembly to PowerShell and use it’s methods. Let’s start it by obtaining core assembly required for Application Insight logging which is Microsoft.ApplicationInsights.

You can get it simply by creating a sample console application and installing an Application Insights nugget in it.

Install-Package Microsoft.ApplicationInsights -Version 2.0.1

Once the nuget is installed, Microsoft.ApplicationInsights assembly will be added in your console application references. Right click on it and navigate to by copying its path. Copy the assembly and paste it in folder where you will be creating your PowerShell commands.

Next step is to create Azure application insights service which will be used as logging repository. You can use azure portal to create it. Very well step by step documentation related to same is available here, you can refer to it if you are not familiar with the creation process.

Once it is created, navigate to the properties tab and copy instrumentation key. Now open up PowerShell editor and load application insight assembly into it.

  1. Clear-Host   
  2. Add-Type -Path "[Your_Assembly_Path]\Microsoft.ApplicationInsights.dll"  

Now since we have loaded the .NET assembly in PowerShell, we can create object of underlying classes in the assembly and access all public methods of those classes.

Here is how you can create object of Telemetry client and assign Application Insight’s instrumentation key to it.

  1. $client = New-Object Microsoft.ApplicationInsights.TelemetryClient  
  2. $client.InstrumentationKey="xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"  

And now the telemetry client is aware of the application insight service where it needs to log all the information. So let’s see how we can use it to log specific information.

Tracking Events

Event tracking is simple and can be done as,

  1. $eventName = "Hello Event"  
  2. $client.TrackEvent($eventName, $null)   

Hello event will be logged to the Applications insights. You can see the logged data either by going to Azure portal or in VS 2015.

In VS 2015 – click on View > Other Windows > Application Insights Search,

Tracking Trace

You can use TrackTrace method to track and analyze the trace information.

  1. $client.TrackTrace("Hello Trace")   

Tracking Exceptions

This is the interesting and cool feature using which you can log your entire exception to Azure AppInsights and use it for further analysis.

Let’s assume a scenario wherein using PowerShell we will try to read a file on the file system which doesn’t exists so that exception is thrown.

  1. try  
  2. {  
  3.     $fileContent = Get-Content -Path "C:\Test.txt" -ErrorAction Stop  
  4. }  
  5. catch  
  6. {  
  7.     $telemtryException = New-Object "Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry"  
  8.     $telemtryException.Exception = $_.Exception  
  9.     $client.TrackException($telemtryException)  
  10. }   

You can observe that exception has been logged and details can be analyzed. Thanks for reading the article, hope you find it useful. Feel free to post your views or comments.

Similar Articles