|
| 1 | +function Measure-CippTask { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Measure and track CIPP task execution with Application Insights telemetry |
| 5 | + .DESCRIPTION |
| 6 | + Wraps task execution in a timer, sends custom event to Application Insights with duration and metadata |
| 7 | + .PARAMETER TaskName |
| 8 | + The name of the task being executed (e.g., "New-CIPPTemplateRun") |
| 9 | + .PARAMETER Script |
| 10 | + The scriptblock to execute and measure |
| 11 | + .PARAMETER Metadata |
| 12 | + Optional hashtable of metadata to include in telemetry (e.g., Command, Tenant, TaskInfo) |
| 13 | + .PARAMETER EventName |
| 14 | + Optional custom event name (default: "CIPP.TaskCompleted") |
| 15 | + .FUNCTIONALITY |
| 16 | + Internal |
| 17 | + .EXAMPLE |
| 18 | + Measure-CippTask -TaskName "ApplyTemplate" -Script { |
| 19 | + # Task logic here |
| 20 | + } -Metadata @{ |
| 21 | + Command = "New-CIPPTemplateRun" |
| 22 | + Tenant = "contoso.onmicrosoft.com" |
| 23 | + } |
| 24 | + .EXAMPLE |
| 25 | + Measure-CippTask -TaskName "DisableGuests" -EventName "CIPP.StandardCompleted" -Script { |
| 26 | + # Standard logic here |
| 27 | + } -Metadata @{ |
| 28 | + Standard = "DisableGuests" |
| 29 | + Tenant = "contoso.onmicrosoft.com" |
| 30 | + } |
| 31 | + #> |
| 32 | + [CmdletBinding()] |
| 33 | + param( |
| 34 | + [Parameter(Mandatory = $true)] |
| 35 | + [string]$TaskName, |
| 36 | + |
| 37 | + [Parameter(Mandatory = $true)] |
| 38 | + [scriptblock]$Script, |
| 39 | + |
| 40 | + [Parameter(Mandatory = $false)] |
| 41 | + [hashtable]$Metadata, |
| 42 | + |
| 43 | + [Parameter(Mandatory = $false)] |
| 44 | + [string]$EventName = 'CIPP.TaskCompleted' |
| 45 | + ) |
| 46 | + |
| 47 | + # Initialize tracking variables |
| 48 | + $sw = [System.Diagnostics.Stopwatch]::StartNew() |
| 49 | + $result = $null |
| 50 | + $errorOccurred = $false |
| 51 | + $errorMessage = $null |
| 52 | + |
| 53 | + try { |
| 54 | + # Execute the actual task |
| 55 | + $result = & $Script |
| 56 | + } catch { |
| 57 | + $errorOccurred = $true |
| 58 | + $errorMessage = $_.Exception.Message |
| 59 | + # Re-throw to preserve original error behavior |
| 60 | + throw |
| 61 | + } finally { |
| 62 | + # Stop the timer |
| 63 | + $sw.Stop() |
| 64 | + $durationMs = [int]$sw.Elapsed.TotalMilliseconds |
| 65 | + |
| 66 | + # Send telemetry if TelemetryClient is available |
| 67 | + if ($global:TelemetryClient) { |
| 68 | + try { |
| 69 | + # Build properties dictionary for customDimensions |
| 70 | + $props = New-Object 'System.Collections.Generic.Dictionary[string,string]' |
| 71 | + $props['TaskName'] = $TaskName |
| 72 | + $props['Success'] = (-not $errorOccurred).ToString() |
| 73 | + |
| 74 | + if ($errorOccurred) { |
| 75 | + $props['ErrorMessage'] = $errorMessage |
| 76 | + } |
| 77 | + |
| 78 | + # Add all metadata to properties |
| 79 | + if ($Metadata) { |
| 80 | + foreach ($key in $Metadata.Keys) { |
| 81 | + $value = $Metadata[$key] |
| 82 | + # Convert value to string, handling nulls |
| 83 | + if ($null -ne $value) { |
| 84 | + $props[$key] = [string]$value |
| 85 | + } else { |
| 86 | + $props[$key] = '' |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # Metrics dictionary for customMeasurements |
| 92 | + $metrics = New-Object 'System.Collections.Generic.Dictionary[string,double]' |
| 93 | + $metrics['DurationMs'] = [double]$durationMs |
| 94 | + |
| 95 | + # Send custom event to Application Insights |
| 96 | + $global:TelemetryClient.TrackEvent($EventName, $props, $metrics) |
| 97 | + $global:TelemetryClient.Flush() |
| 98 | + |
| 99 | + Write-Verbose "Telemetry sent for task '$TaskName' to event '$EventName' (${durationMs}ms)" |
| 100 | + } catch { |
| 101 | + Write-Warning "Failed to send telemetry for task '${TaskName}': $($_.Exception.Message)" |
| 102 | + } |
| 103 | + } else { |
| 104 | + Write-Verbose "TelemetryClient not initialized, skipping telemetry for task '$TaskName'" |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return $result |
| 109 | +} |
0 commit comments