Skip to content

Commit 71f94c8

Browse files
authored
Get correct PSVersion from PowerShell runtime (#278)
1 parent 92c73a2 commit 71f94c8

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/Common/AzurePSCmdlet.cs

+27-24
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,12 @@ protected virtual bool IsErrorMetricEnabled
101101
get { return true; }
102102
}
103103

104-
/// <summary>
105-
/// Indicates installed PowerShell version
106-
/// </summary>
107-
private string _psVersion;
108-
109-
/// <summary>
110-
/// Get PsVersion returned from PowerShell.Runspace instance
111-
/// </summary>
104+
[Obsolete("Should use AzurePSCmdlet.PowerShellVersion")]
112105
protected string PSVersion
113106
{
114107
get
115108
{
116-
if (string.IsNullOrEmpty(_psVersion))
117-
{
118-
if (this.Host != null)
119-
{
120-
_psVersion = this.Host.Version.ToString();
121-
}
122-
else
123-
{
124-
//We are doing this for perf. reasons. This code will execute during tests and so reducing the perf. overhead while running tests.
125-
_psVersion = DEFAULT_PSVERSION;
126-
}
127-
}
128-
129-
return _psVersion;
109+
return LoadPowerShellVersion();
130110
}
131111
}
132112

@@ -323,7 +303,7 @@ protected virtual void TearDownDebuggingTraces()
323303
protected virtual void SetupHttpClientPipeline()
324304
{
325305
AzureSession.Instance.ClientFactory.AddUserAgent(ModuleName, string.Format("v{0}", AzVersion));
326-
AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PSVersion));
306+
AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PowerShellVersion));
327307

328308
AzureSession.Instance.ClientFactory.AddHandler(
329309
new CmdletInfoHandler(this.CommandRuntime.ToString(),
@@ -637,9 +617,16 @@ protected virtual void InitializeQosEvent()
637617
string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
638618
if (!String.IsNullOrWhiteSpace(hostEnv))
639619
UserAgent += string.Format(" {0}", hostEnv.Trim());
620+
PowerShellVersion = this.LoadPowerShellVersion();
621+
PSHostName = this.Host?.Name;
622+
PSHostVersion = this.Host?.Version?.ToString();
640623
}
624+
641625
_qosEvent.AzVersion = AzVersion;
642626
_qosEvent.UserAgent = UserAgent;
627+
_qosEvent.PSVersion = PowerShellVersion;
628+
_qosEvent.HostVersion = PSHostVersion;
629+
_qosEvent.PSHostName = PSHostName;
643630

644631
if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
645632
{
@@ -743,7 +730,6 @@ protected void LogQosEvent()
743730

744731
try
745732
{
746-
_metricHelper.SetPSHost(this.Host);
747733
_metricHelper.LogQoSEvent(_qosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
748734
_metricHelper.FlushMetric();
749735
WriteDebug("Finish sending metric.");
@@ -977,6 +963,9 @@ public virtual bool IsTerminatingError(Exception ex)
977963
//Initialized once AzVersion is loaded.
978964
//Format: AzurePowershell/Az0.0.0 %AZUREPS_HOST_ENVIROMENT%
979965
public static string UserAgent { set; get; }
966+
public static string PowerShellVersion { set; get; }
967+
public static string PSHostVersion { set; get; }
968+
public static string PSHostName { set; get; }
980969

981970
protected string LoadAzVersion()
982971
{
@@ -1024,5 +1013,19 @@ protected string LoadAzVersion()
10241013
WriteDebug(string.Format("Sought all Az modules and got latest version {0}", ret));
10251014
return ret;
10261015
}
1016+
1017+
private string LoadPowerShellVersion()
1018+
{
1019+
try
1020+
{
1021+
var outputs = this.ExecuteScript<string>("$Host.Runspace.Version.ToString()");
1022+
return outputs[0];
1023+
}
1024+
catch (Exception e)
1025+
{
1026+
WriteDebug(string.Format("Cannot fetch PowerShell version due to exception: {0}", e.Message));
1027+
}
1028+
return DEFAULT_PSVERSION;
1029+
}
10271030
}
10281031
}

src/Common/MetricHelper.cs

+18-20
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,14 @@ public class MetricHelper
6666

6767
private AzurePSDataCollectionProfile _profile;
6868

69-
private static PSHost _host;
70-
71-
private static string _psVersion;
72-
69+
[Obsolete("Should use AzurePSCmdlet.PowerShellVersion")]
7370
protected string PSVersion
7471
{
7572
get
7673
{
77-
if (_host != null)
78-
{
79-
_psVersion = _host.Version.ToString();
80-
}
81-
else
82-
{
83-
_psVersion = DefaultPSVersion;
84-
}
85-
return _psVersion;
74+
return DefaultPSVersion;
8675
}
8776
}
88-
8977
public string HashMacAddress
9078
{
9179
get
@@ -256,9 +244,9 @@ private void LoadTelemetryClientContext(AzurePSQoSEvent qos, TelemetryContext cl
256244
}
257245
}
258246

247+
[Obsolete()]
259248
public void SetPSHost(PSHost host)
260249
{
261-
_host = host;
262250
}
263251

264252
private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string, string> eventProperties, bool populateException = false)
@@ -268,18 +256,22 @@ private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string,
268256
return;
269257
}
270258

271-
eventProperties.Add("telemetry-version", "1");
259+
// Breaking change of telemetry
260+
// * 2, change host version to real PowerShell version. Original version was PowerShell host version which is not always the same as PS version
261+
// and can be customized.
262+
eventProperties.Add("telemetry-version", "2");
272263
eventProperties.Add("Command", qos.CommandName);
273264
eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
274265
eventProperties.Add("ModuleName", qos.ModuleName);
275266
eventProperties.Add("ModuleVersion", qos.ModuleVersion);
276267
eventProperties.Add("HostVersion", qos.HostVersion);
268+
eventProperties.Add("HostName", qos.PSHostName);
277269
eventProperties.Add("OS", Environment.OSVersion.ToString());
278270
eventProperties.Add("CommandParameters", qos.Parameters);
279271
eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
280272
eventProperties.Add("UserAgent", qos.UserAgent);
281273
eventProperties.Add("HashMacAddress", HashMacAddress);
282-
eventProperties.Add("PowerShellVersion", PSVersion);
274+
eventProperties.Add("PowerShellVersion", qos.PSVersion);
283275
eventProperties.Add("Version", qos.AzVersion);
284276
eventProperties.Add("CommandParameterSetName", qos.ParameterSetName);
285277
eventProperties.Add("CommandInvocationName", qos.InvocationName);
@@ -538,7 +530,12 @@ public class AzurePSQoSEvent
538530
public string CommandName { get; set; }
539531
public string ModuleName { get; set; }
540532
public string ModuleVersion { get; set; }
533+
//Version of PowerShell runspace ($Host.Runspace.Version)
534+
public string PSVersion { get; set; }
535+
//Host version of PowerShell ($Host.Version) which can be customized by PowerShell wrapper
541536
public string HostVersion { get; set; }
537+
//Host Name of PowerShell
538+
public string PSHostName { get; set; }
542539
public string AzVersion { get; set; }
543540
public string UserAgent { get; set; }
544541
public string Parameters { get; set; }
@@ -583,11 +580,12 @@ public void FinishQosEvent()
583580

584581
public override string ToString()
585582
{
586-
string ret = string.Format(
587-
"AzureQoSEvent: CommandName - {0}; IsSuccess - {1}; Duration - {2}", CommandName, IsSuccess, Duration);
583+
string ret = $"AzureQoSEvent: Module: {ModuleName}:{ModuleVersion}; CommandName: {CommandName}; PSVersion: {PSVersion}";
584+
ret += $"; IsSuccess: {IsSuccess}; Duration: {Duration}";
585+
588586
if (Exception != null)
589587
{
590-
ret = $"{ret}; Exception - {Exception.Message};";
588+
ret += $"; Exception: {Exception.Message};";
591589
}
592590
return ret;
593591
}

0 commit comments

Comments
 (0)