Skip to content

Commit 5a94020

Browse files
authored
Merge pull request #3154 from Azure/psVerRel310
Adding PsVersion to userAgent
2 parents 8d62464 + 2d9df3e commit 5a94020

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
3535
/// </summary>
3636
public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
3737
{
38+
private const string PSVERSION = "PSVersion";
39+
private const string DEFAULT_PSVERSION = "3.0.0.0";
40+
3841
public ConcurrentQueue<string> DebugMessages { get; private set; }
3942

4043
private RecordingTracingInterceptor _httpTracingInterceptor;
@@ -57,6 +60,35 @@ protected virtual bool IsErrorMetricEnabled
5760
get { return true; }
5861
}
5962

63+
/// <summary>
64+
/// Indicates installed PowerShell version
65+
/// </summary>
66+
private string _psVersion;
67+
68+
/// <summary>
69+
/// Get PsVersion returned from PowerShell.Runspace instance
70+
/// </summary>
71+
protected string PSVersion
72+
{
73+
get
74+
{
75+
if (string.IsNullOrEmpty(_psVersion))
76+
{
77+
if(this.Host != null)
78+
{
79+
_psVersion = this.Host.Version.ToString();
80+
}
81+
else
82+
{
83+
//We are doing this for perf. reasons. This code will execute during tests and so reducing the perf. overhead while running tests.
84+
_psVersion = DEFAULT_PSVERSION;
85+
}
86+
}
87+
88+
return _psVersion;
89+
}
90+
}
91+
6092
/// <summary>
6193
/// Gets the PowerShell module name used for user agent header.
6294
/// By default uses "Azure PowerShell"
@@ -243,9 +275,9 @@ protected virtual void TearDownDebuggingTraces()
243275

244276
protected virtual void SetupHttpClientPipeline()
245277
{
246-
ProductInfoHeaderValue userAgentValue = new ProductInfoHeaderValue(
247-
ModuleName, string.Format("v{0}", ModuleVersion));
248-
AzureSession.ClientFactory.UserAgents.Add(userAgentValue);
278+
AzureSession.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(ModuleName, string.Format("v{0}", ModuleVersion)));
279+
AzureSession.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(PSVERSION, string.Format("v{0}", PSVersion)));
280+
249281
AzureSession.ClientFactory.AddHandler(
250282
new CmdletInfoHandler(this.CommandRuntime.ToString(),
251283
this.ParameterSetName, this._clientRequestId));

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
<Compile Include="AzurePSDataCollectionProfile.cs" />
140140
<Compile Include="AzurePowerShell.cs" />
141141
<Compile Include="AzureRmProfileProvider.cs" />
142-
<Compile Include="AzureSMProfileProvder.cs" />
142+
<Compile Include="AzureSMProfileProvider.cs" />
143143
<Compile Include="ConcurrentQueueExtensions.cs" />
144144
<Compile Include="Constants.cs" />
145145
<Compile Include="ContextExtensions.cs" />

src/ResourceManager/Profile/Commands.Profile.Test/LoginCmdletTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
using System.Reflection;
2424
using Xunit;
2525
using Xunit.Abstractions;
26+
using System.Collections.Generic;
27+
using System.Net.Http.Headers;
28+
using System.Diagnostics;
2629

2730
namespace Microsoft.Azure.Commands.Profile.Test
2831
{
@@ -40,6 +43,42 @@ public LoginCmdletTests(ITestOutputHelper output)
4043
AzureRmProfileProvider.Instance.Profile = new AzureRMProfile();
4144
}
4245

46+
[Fact]
47+
[Trait(Category.AcceptanceType, Category.CheckIn)]
48+
public void GetPsVersionFromUserAgent()
49+
{
50+
var cmdlt = new AddAzureRMAccountCommand();
51+
52+
int preProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
53+
Debug.WriteLine("UserAgents count prior to cmdLet processing = {0}", preProcessingUserAgentCount.ToString());
54+
foreach (ProductInfoHeaderValue hv in AzureSession.ClientFactory.UserAgents)
55+
{
56+
Debug.WriteLine("Product:{0} - Version:{1}", hv.Product.Name, hv.Product.Version);
57+
}
58+
59+
cmdlt.CommandRuntime = commandRuntimeMock;
60+
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
61+
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
62+
63+
cmdlt.InvokeBeginProcessing();
64+
int postProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
65+
Debug.WriteLine("UserAgents count prior to cmdLet post processing = {0}", postProcessingUserAgentCount.ToString());
66+
Assert.True(AzureSession.ClientFactory.UserAgents.Count >= preProcessingUserAgentCount);
67+
HashSet<ProductInfoHeaderValue> piHv = AzureSession.ClientFactory.UserAgents;
68+
string psUserAgentString = string.Empty;
69+
70+
foreach(ProductInfoHeaderValue hv in piHv)
71+
{
72+
if(hv.Product.Name.Equals("PSVersion") && (!string.IsNullOrEmpty(hv.Product.Version)))
73+
{
74+
psUserAgentString = string.Format("{0}-{1}", hv.Product.Name, hv.Product.Version);
75+
}
76+
}
77+
78+
Assert.NotEmpty(psUserAgentString);
79+
Assert.Contains("PSVersion", psUserAgentString);
80+
}
81+
4382
[Fact]
4483
[Trait(Category.RunType, Category.LiveOnly)]
4584
public void LoginWithSubscriptionAndTenant()

0 commit comments

Comments
 (0)