Skip to content

LeonB87/PSCoreApplicationInsights

Repository files navigation

PSCoreApplicationInsightst

ApplicationInsights Module

Powershell logging to Azure Application Insight

A Powershell Core Module that simplifies logging to Application Insights. This uses the Built-in Telemetry client from Powershell Core 7 and it is built entirely in Powershell. It offers a few simple functions to log information to Application Insights to reduce the clutter in your scripts.

Installation

Install-Module -Name PSCoreApplicationInsights

Basic Usage

Create a new Application Insights Client

New-ApplicationInsightsClient [-InstrumentationKey] <Guid> [-WhatIf] [-Confirm] [<CommonParameters>]

Use the Instrumentation Key found in the Azure Portal on your Application Insights Instance.

The Application Insights client is stored as $global:AIClient.

to store the client in a variable to specify when writing logs:

$client = New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000

Sending Trace information application insights

Syntax

Write-ApplicationInsightsTrace [[-Client] <TelemetryClient>] [-Message] <String> [[-SeverityLevel] <String>] [[-properties] <System.Collections.Generic.Dictionary`2[System.String,System.String]>] [<CommonParameters>]
Property Description Mandatory default Allowed Values
Message true
Client The Application Insights client to write the message to. If not specifies, uses the $global:AIclient false
SeverityLevel The severity level of the trace false Information - Information
- Verbose
- Warning
- Error
- Critical
properties a Dictionary<string,string> with custom properties that will be added as "customDimensions" false

Example 1

Write-ApplicationInsightsTrace -Message "This is a test message as Critical" -SeverityLevel "Critical"

Result:

image

Example 2

$properties = [System.Collections.Generic.Dictionary[string, string]]::new()

$properties.Add("target", "azkv-powershell-001")
$properties.Add("type", "Keyvault")

Write-ApplicationInsightsTrace -Client $client -Message "Created new keyvault" -SeverityLevel "Information" -properties $properties

Result:

image

Module Functions

New-ApplicationInsightsClient

Description

Create a new Application insights Client by supplying an Instrumentation Key of your Application Insights instance.

Syntax

 New-ApplicationInsightsClient [-InstrumentationKey] <Guid> [-WhatIf] [-Confirm] [<CommonParameters>]

Examples

Example 1
 New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000
Example 2
 $client = New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000

Parameters

InstrumentationKey

The Instrumentation Key of your Application Insights instance.

Type: Guid
PipelineInput : false
Position : 1
Required : true

Invoke-ApplicationInsightsMeasuredCommand

Description

Invoke a scriptblock that is measured by Application Insights. This created a timespan and writes the timing to Application Insights. The output of the scriptblock is returned.

Syntax

 Invoke-ApplicationInsightsMeasuredCommand [[-Client] <TelemetryClient>] [-scriptblock] <ScriptBlock> [-name] <String> [<CommonParameters>]

Examples

Example 1
 Invoke-ApplicationInsightsMeasuredCommand -ScriptBlock { start-sleep -seconds 1 } -Name "slow script"

Parameters

Client

The Application Insights Telemetry Client. Defaults to $global:AIClient

Type: TelemetryClient
PipelineInput : false
Position : 1
Required : false

results

Example Logs:

image

Example of the Performance blade

image

Performance is shown on the Overview Blade

image

TODO

  • Automate Deployment
  • Write Documentation for each function
    • Creating a new Application Insights Client
    • Setting Client information
    • Sending Trace
    • Sending Metric
    • Sending Exception
    • Sending Request
    • Invoking Measured Command
  • Create an example azure dashboard

About

A Powershell Core Module that simplifies logging to Application Insights

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published