Framework/Helpers/AIOrgTelemetryHelper.ps1

Set-StrictMode -Version Latest

class AIOrgTelemetryHelper {
    static hidden [string[]] $ParamsToMask = @("OMSSharedKey");

    static hidden [Microsoft.ApplicationInsights.TelemetryClient] $OrgTelemetryClient;
    static hidden [Microsoft.ApplicationInsights.TelemetryClient] $UsageTelemetryClient;

    static AIOrgTelemetryHelper() {
        [AIOrgTelemetryHelper]::OrgTelemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
    }

    static [void] TrackEvent([string] $Name) {
        [AIOrgTelemetryHelper]::TrackEvent($Name, $null, $null);
    }

    static [void] TrackEventWithOnlyProperties([string] $Name, [hashtable] $Properties) {
        [AIOrgTelemetryHelper]::TrackEvent($Name, $Properties, $null);
    }

    static [void] TrackEventWithOnlyMetrics([string] $Name, [hashtable] $Metrics) {
        [AIOrgTelemetryHelper]::TrackEvent($Name, $null, $Metrics);
    }

    static [void] TrackEvent([string] $Name, [hashtable] $Properties, [hashtable] $Metrics) {
        if (![RemoteReportHelper]::IsAIOrgTelemetryEnabled()) { return; };
        [AIOrgTelemetryHelper]::TrackEventInternal($Name, $Properties, $Metrics);
        [AIOrgTelemetryHelper]::OrgTelemetryClient.Flush();
    }

    static [void] TrackEvents([System.Collections.ArrayList] $events) {
        if (![RemoteReportHelper]::IsAIOrgTelemetryEnabled()) { return; };
        foreach ($item in $events) {
            [AIOrgTelemetryHelper]::TrackEventInternal($item.Name, $item.Properties, $item.Metrics);
        }
        [AIOrgTelemetryHelper]::OrgTelemetryClient.Flush();
    }

    static [void] TrackCommandExecution([string] $Name, [hashtable] $Properties, [hashtable] $Metrics, [System.Management.Automation.InvocationInfo] $invocationContext) {
        if (![RemoteReportHelper]::IsAIOrgTelemetryEnabled()) { return; };
        $Properties = [AIOrgTelemetryHelper]::AttachInvocationInfo($Properties, $invocationContext);
        [AIOrgTelemetryHelper]::TrackEventInternal($Name, $Properties, $Metrics);
        [AIOrgTelemetryHelper]::OrgTelemetryClient.Flush();
    }

    static [void] TrackException([System.Management.Automation.ErrorRecord] $ErrorRecord, [System.Management.Automation.InvocationInfo] $InvocationContext) {
        [AIOrgTelemetryHelper]::TrackException($ErrorRecord, $null, $null, $InvocationContext);
    }

    static [void] TrackExceptionWithOnlyProperties([System.Management.Automation.ErrorRecord] $ErrorRecord, [hashtable] $Properties, [System.Management.Automation.InvocationInfo] $InvocationContext) {
        [AIOrgTelemetryHelper]::TrackException($ErrorRecord, $Properties, $null, $InvocationContext);
    }

    static [void] TrackExceptionWithOnlyMetrics([System.Management.Automation.ErrorRecord] $ErrorRecord, [hashtable] $Metrics, [System.Management.Automation.InvocationInfo] $InvocationContext) {
        [AIOrgTelemetryHelper]::TrackException($ErrorRecord, $null, $Metrics, $InvocationContext);
    }

    static [void] TrackException([System.Management.Automation.ErrorRecord] $ErrorRecord, [hashtable] $Properties, [hashtable] $Metrics, [System.Management.Automation.InvocationInfo] $InvocationContext) {
        try {
            if (![RemoteReportHelper]::IsAIOrgTelemetryEnabled()) { return; };
            $Properties = [AIOrgTelemetryHelper]::AttachInvocationInfo($Properties, $InvocationContext);
            $Properties = [AIOrgTelemetryHelper]::AttachCommonProperties($Properties);
            $Metrics = [AIOrgTelemetryHelper]::AttachCommonMetrics($Metrics);
            $ex = [Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry]::new()
            $ex.Exception = $ErrorRecord.Exception
            try{
                $ex.Properties.Add("ScriptStackTrace", $ErrorRecord.ScriptStackTrace)
            }catch{}
            $Properties.Keys | ForEach-Object {
                try{
                    $ex.Properties.Add($_, $Properties[$_].ToString());
                }
                catch{
                }
            }
            $Metrics.Keys | ForEach-Object {
                try{
                    $ex.Metrics.Add($_, $Metrics[$_]);
                }
                catch{
                }
            }
            [AIOrgTelemetryHelper]::OrgTelemetryClient.InstrumentationKey = [RemoteReportHelper]::GetAIOrgTelemetryKey();
            [AIOrgTelemetryHelper]::OrgTelemetryClient.TrackException($ex);
            [AIOrgTelemetryHelper]::OrgTelemetryClient.Flush();
        }
        catch {
        }
    }


    hidden static [void] TrackEventInternal([string] $Name, [hashtable] $Properties, [hashtable] $Metrics) {
        $Properties = [AIOrgTelemetryHelper]::AttachCommonProperties($Properties);
        $Metrics = [AIOrgTelemetryHelper]::AttachCommonMetrics($Metrics);
        try {
            $event = [Microsoft.ApplicationInsights.DataContracts.EventTelemetry]::new()
            $event.Name = $Name
            $Properties.Keys | ForEach-Object {
                try {
                    $event.Properties.Add($_, ($Properties[$_].ToString()));
                }
                catch {
                }
            }
            $Metrics.Keys | ForEach-Object {
                try {
                    $event.Metrics.Add($_, $Metrics[$_]);
                }
                catch {
                }
            }
            [AIOrgTelemetryHelper]::OrgTelemetryClient.InstrumentationKey = [RemoteReportHelper]::GetAIOrgTelemetryKey();
            [AIOrgTelemetryHelper]::OrgTelemetryClient.TrackEvent($event);
        }
        catch {
        }
    }

    hidden static [hashtable] AttachCommonProperties([hashtable] $Properties) {
        if ($Properties -eq $null) {
            $Properties = @{}
        }
        else {
            $Properties = $Properties.Clone()
        }
        try {
            $NA = "NA";
            try {
                $Properties.Add("ScanSource", [RemoteReportHelper]::GetScanSource());
            }
            catch {}
            try {
                $module = Get-Module 'AzSDK*' | Select-Object -First 1
                $Properties.Add("ScannerModuleName", $module.Name);
                $Properties.Add("ScannerVersion", $module.Version.ToString());
            }
            catch {}
            try {
                $azureContext = Get-AzureRmContext
                try {
                    $Properties.Add([TelemetryKeys]::SubscriptionId, $azureContext.Subscription.Id)
                }
                catch {}
                try {
                    $Properties.Add([TelemetryKeys]::SubscriptionName, $azureContext.Subscription.Name)
                }
                catch {}
                try {
                    $Properties.Add("AzureEnv", $azureContext.Environment.Name)
                }
                catch {}
                try {
                    $Properties.Add("TenantId", $azureContext.Tenant.Id)
                }
                catch {}
                try {
                    $Properties.Add("AccountId", $azureContext.Account.Id)
                }
                catch {}
                try {
                    if ($Properties.ContainsKey("RunIdentifier")) {
                        $actualRunId = $Properties["RunIdentifier"]
                        $Properties["UniqueRunIdentifier"] = [RemoteReportHelper]::Mask($azureContext.Account.Id + '##' + $actualRunId.ToString())
                    }
                }
                catch {}
                try {
                    $Properties.Add("AccountType", $azureContext.Account.Type);
                }
                catch {}
            }
            catch {
            }
        }
        catch {
        }
        return $Properties;
    }

    hidden static [hashtable] AttachCommonMetrics([hashtable] $Metrics) {
        if ($Metrics -eq $null) {
            $Metrics = @{}
        }
        else {
            $Metrics = $Metrics.Clone()
        }
        return $Metrics;
    }

    hidden static [hashtable] AttachInvocationInfo([hashtable] $Properties, [System.Management.Automation.InvocationInfo] $invocationContext) {
        if ($Properties -eq $null) {
            $Properties = @{}
        }
        else {
            $Properties = $Properties.Clone()
        }
        if ($invocationContext -eq $null) { return $Properties};
        $Properties.Add("Command", $invocationContext.MyCommand.Name)
        $params = @{}
        $invocationContext.BoundParameters.Keys | ForEach-Object {
            $value = "MASKED"
            if (![AIOrgTelemetryHelper]::ParamsToMask.Contains($_)) {
                $value = $invocationContext.BoundParameters[$_].ToString()
            }
            $Properties.Add("Param" + $_, $value)
            $params.Add("$_", $value)
        }
        $Properties.Add("Params", [Helpers]::ConvertToJsonCustomCompressed($params))
        $loadedModules = Get-Module | % { $_.Name + "=" + $_.Version.ToString()}
        $Properties.Add("LoadedModules" , ($loadedModules -join ';'))
        return $Properties;
    }
}