@@ -322,6 +322,17 @@ protected virtual void SetupHttpClientPipeline()
322322 AzureSession . Instance . ClientFactory . AddUserAgent ( "AzurePowershell" , string . Format ( "v{0}" , AzVersion ) ) ;
323323 AzureSession . Instance . ClientFactory . AddUserAgent ( PSVERSION , string . Format ( "v{0}" , PowerShellVersion ) ) ;
324324 AzureSession . Instance . ClientFactory . AddUserAgent ( ModuleName , this . ModuleVersion ) ;
325+ try {
326+ string hostEnv = AzurePSCmdlet . getEnvUserAgent ( ) ;
327+ if ( ! String . IsNullOrWhiteSpace ( hostEnv ) )
328+ {
329+ AzureSession . Instance . ClientFactory . AddUserAgent ( hostEnv ) ;
330+ }
331+ }
332+ catch ( Exception )
333+ {
334+ // ignore if it failed.
335+ }
325336
326337 AzureSession . Instance . ClientFactory . AddHandler (
327338 new CmdletInfoHandler ( this . CommandRuntime . ToString ( ) ,
@@ -331,6 +342,18 @@ protected virtual void SetupHttpClientPipeline()
331342
332343 protected virtual void TearDownHttpClientPipeline ( )
333344 {
345+ try
346+ {
347+ string hostEnv = AzurePSCmdlet . getEnvUserAgent ( ) ;
348+ if ( ! String . IsNullOrWhiteSpace ( hostEnv ) )
349+ {
350+ AzureSession . Instance . ClientFactory . RemoveUserAgent ( hostEnv ) ;
351+ }
352+ }
353+ catch ( Exception )
354+ {
355+ // ignore if it failed.
356+ }
334357 AzureSession . Instance . ClientFactory . RemoveUserAgent ( ModuleName ) ;
335358 AzureSession . Instance . ClientFactory . RemoveHandler ( typeof ( CmdletInfoHandler ) ) ;
336359 }
@@ -721,14 +744,14 @@ protected virtual void InitializeQosEvent()
721744 if ( AzVersion == null )
722745 {
723746 AzVersion = this . LoadModuleVersion ( "Az" , true ) ;
724- UserAgent = new ProductInfoHeaderValue ( "AzurePowershell" , string . Format ( "Az{0}" , AzVersion ) ) . ToString ( ) ;
725- string hostEnv = Environment . GetEnvironmentVariable ( "AZUREPS_HOST_ENVIRONMENT" ) ;
726- if ( ! String . IsNullOrWhiteSpace ( hostEnv ) )
727- UserAgent += string . Format ( " {0}" , hostEnv . Trim ( ) ) ;
728747 PowerShellVersion = this . LoadPowerShellVersion ( ) ;
729748 PSHostName = this . Host ? . Name ;
730749 PSHostVersion = this . Host ? . Version ? . ToString ( ) ;
731750 }
751+ UserAgent = new ProductInfoHeaderValue ( "AzurePowershell" , string . Format ( "Az{0}" , AzVersion ) ) . ToString ( ) ;
752+ string hostEnv = AzurePSCmdlet . getEnvUserAgent ( ) ;
753+ if ( ! String . IsNullOrWhiteSpace ( hostEnv ) )
754+ UserAgent += string . Format ( " {0}" , hostEnv ) ;
732755 if ( AzAccountsVersion == null )
733756 {
734757 AzAccountsVersion = this . LoadModuleVersion ( "Az.Accounts" , false ) ;
@@ -777,6 +800,19 @@ protected virtual void InitializeQosEvent()
777800 _qosEvent . SanitizerInfo = new SanitizerTelemetry ( OutputSanitizer ? . RequireSecretsDetection == true ) ;
778801 }
779802
803+ private static string getEnvUserAgent ( )
804+ {
805+ string hostEnv = Environment . GetEnvironmentVariable ( "AZUREPS_HOST_ENVIRONMENT" ) ;
806+ if ( String . IsNullOrWhiteSpace ( hostEnv ) )
807+ {
808+ return null ;
809+ }
810+ else
811+ {
812+ return hostEnv . Trim ( ) ;
813+ }
814+ }
815+
780816 private void RecordDebugMessages ( )
781817 {
782818 try
0 commit comments