EventMonitor/Telemetry/AITelemetry.psm1
<#PSScriptInfo
.AUTHOR Microsoft .COMPANYNAME Microsoft Corporation .COPYRIGHT (c) Microsoft Corporation #> using namespace Microsoft.ApplicationInsights function TrackEvent { param( [Parameter(Mandatory = $true)] [string] $logAnalyticsConString, [parameter(Mandatory=$True)] [string] $Name, [parameter(Mandatory=$false)] [system.collections.generic.dictionary[[string], [string]]] $Properties, [parameter(Mandatory=$false)] [system.collections.generic.dictionary[[string], [double]]] $Metrics ) try { Add-Type -Path "$PSScriptRoot\Microsoft.ApplicationInsights.dll" $tc = New-Object Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration $tc.ConnectionString = $logAnalyticsConString; $client = New-Object "Microsoft.ApplicationInsights.TelemetryClient" -Argument $tc $client.TrackEvent($Name, $Properties, $Metrics); } catch { Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: Exception Message: `n$_.Exception.Message" Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: ScriptStackTrace: `n$_.ScriptStackTrace" $LASTEXITCODE = 1 $ErrorProps = New-Object 'system.collections.generic.dictionary[string, string]' $ErrorProps.Add("Name", "$Name") $ErrorProps.Add("Function", "TrackEvent") TrackException -ErrorRecord $_ -Properties $ErrorProps -logAnalyticsConString $logAnalyticsConString; } finally { $client.Flush() $tc.Dispose() } } function TrackTrace { param( [Parameter(Mandatory = $true)] [string] $logAnalyticsConString, [parameter(Mandatory=$True)] [string] $Message, [parameter(Mandatory=$false)] [system.collections.generic.dictionary[[string], [string]]] $Properties ) try { Add-Type -Path "$PSScriptRoot\Microsoft.ApplicationInsights.dll" $tc = New-Object Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration $tc.ConnectionString = $logAnalyticsConString; $client = New-Object "Microsoft.ApplicationInsights.TelemetryClient" -Argument $tc $client.TrackTrace($Message, $Properties) } catch { Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: Exception Message: `n$_.Exception.Message" Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: ScriptStackTrace: `n$_.ScriptStackTrace" $LASTEXITCODE = 1 $ErrorProps = New-Object 'system.collections.generic.dictionary[string, string]' $ErrorProps.Add("Name", "$Message") $ErrorProps.Add("Function", "TrackTrace") TrackException -ErrorRecord $_ -Properties $ErrorProps -logAnalyticsConString $logAnalyticsConString; } finally { $client.Flush() $tc.Dispose() } } function TrackException { param( [Parameter(Mandatory = $true)] [string] $logAnalyticsConString, [parameter(Mandatory=$True)] [System.Management.Automation.ErrorRecord] $ErrorRecord, [parameter(Mandatory=$false)] [system.collections.generic.dictionary[[string], [string]]] $Properties, [parameter(Mandatory=$false)] [system.collections.generic.dictionary[[string], [double]]] $Metrics ) try { Add-Type -Path "$PSScriptRoot\Microsoft.ApplicationInsights.dll" $tc = New-Object Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration $tc.ConnectionString = $logAnalyticsConString; $client = New-Object "Microsoft.ApplicationInsights.TelemetryClient" -Argument $tc $client.TrackException($ErrorRecord.Exception, $Properties, $Metrics); } catch { Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: Exception Message: `n$_.Exception.Message" Add-Content -Path "$PSScriptRoot\Logs.txt" -Value "$(get-date -UFormat %c) :: ScriptStackTrace: `n$_.ScriptStackTrace" $LASTEXITCODE = 1 throw "Failed to track exception: $($_.Exception)" } finally { $client.Flush(); $tc.Dispose() } } |