What is PowerShell.Telemetry.ApplicationInsightsTelemetry and can I turn it off?

Viewed 471

I have a C# testing project that uses the Microsoft.PowerShell.SDK NuGet package. I use it to run a cURL command that I can't seem to get to work with an HttpClient (details on that here if you are interested).

It works great, except when I run as a weak user using RunImpersonated. Then I get the following exception:

System.TypeInitializationException : The type initializer for 'Microsoft.PowerShell.Telemetry.ApplicationInsightsTelemetry' threw an exception.

With an inner exception of:

System.UnauthorizedAccessException : Access to the path 'CreateUniqueUserId' is denied.

I am not sure why my cURL powershell command needs to use Application Insights and I would like to turn it off if possible.

Is it possible to turn off Application Insights with the Powershell NuGet?

1 Answers

From the about_Telemetry help file:

To opt-out of this telemetry, set the environment variable POWERSHELL_TELEMETRY_OPTOUT to true, yes, or 1.

So running this when your application starts should do the trick:

Environment.SetEnvironmentVariable("POWERSHELL_TELEMETRY_OPTOUT", "1");
Related