diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs index 58987b322a3..ac035ba7305 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Telemetry.cs @@ -21,6 +21,7 @@ internal static class ApplicationInsightsTelemetry // The name of the file by when present in $PSHOME will enable telemetry. // If this file is not present, no telemetry will be sent. private const string TelemetrySemaphoreFilename = "DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY"; + private const string TelemetryOptoutEnvVar = "POWERSHELL_TELEMETRY_OPTOUT"; // The path to the semaphore file which enables telemetry private static string TelemetrySemaphoreFilePath = Path.Combine( @@ -42,6 +43,28 @@ static ApplicationInsightsTelemetry() TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = _developerMode; } + private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) { + var str = Environment.GetEnvironmentVariable(name); + if (string.IsNullOrEmpty(str)) + { + return defaultValue; + } + + switch (str.ToLowerInvariant()) + { + case "true": + case "1": + case "yes": + return true; + case "false": + case "0": + case "no": + return false; + default: + return defaultValue; + } + } + /// /// Send the telemetry /// @@ -50,14 +73,18 @@ private static void SendTelemetry(string eventName, Dictionarypay try { // if the semaphore file exists, try to send telemetry - if (Utils.NativeFileExists(TelemetrySemaphoreFilePath)) + var enabled = Utils.NativeFileExists(TelemetrySemaphoreFilePath) && !GetEnvironmentVariableAsBool(TelemetryOptoutEnvVar, false); + + if (!enabled) + { + return; + } + + if (_telemetryClient == null) { - if ( _telemetryClient == null ) - { - _telemetryClient = new TelemetryClient(); - } - _telemetryClient.TrackEvent(eventName, payload, null); + _telemetryClient = new TelemetryClient(); } + _telemetryClient.TrackEvent(eventName, payload, null); } catch (Exception) {