Skip to content

Get correct PSVersion from PowerShell runtime #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,12 @@ protected virtual bool IsErrorMetricEnabled
get { return true; }
}

/// <summary>
/// Indicates installed PowerShell version
/// </summary>
private string _psVersion;

/// <summary>
/// Get PsVersion returned from PowerShell.Runspace instance
/// </summary>
[Obsolete("Should use AzurePSCmdlet.PowerShellVersion")]
protected string PSVersion
{
get
{
if (string.IsNullOrEmpty(_psVersion))
{
if (this.Host != null)
{
_psVersion = this.Host.Version.ToString();
}
else
{
//We are doing this for perf. reasons. This code will execute during tests and so reducing the perf. overhead while running tests.
_psVersion = DEFAULT_PSVERSION;
}
}

return _psVersion;
return LoadPowerShellVersion();
}
}

Expand Down Expand Up @@ -323,7 +303,7 @@ protected virtual void TearDownDebuggingTraces()
protected virtual void SetupHttpClientPipeline()
{
AzureSession.Instance.ClientFactory.AddUserAgent(ModuleName, string.Format("v{0}", AzVersion));
AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PSVersion));
AzureSession.Instance.ClientFactory.AddUserAgent(PSVERSION, string.Format("v{0}", PowerShellVersion));

AzureSession.Instance.ClientFactory.AddHandler(
new CmdletInfoHandler(this.CommandRuntime.ToString(),
Expand Down Expand Up @@ -637,9 +617,16 @@ protected virtual void InitializeQosEvent()
string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
if (!String.IsNullOrWhiteSpace(hostEnv))
UserAgent += string.Format(" {0}", hostEnv.Trim());
PowerShellVersion = this.LoadPowerShellVersion();
PSHostName = this.Host?.Name;
PSHostVersion = this.Host?.Version?.ToString();
}

_qosEvent.AzVersion = AzVersion;
_qosEvent.UserAgent = UserAgent;
_qosEvent.PSVersion = PowerShellVersion;
_qosEvent.HostVersion = PSHostVersion;
_qosEvent.PSHostName = PSHostName;

if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
{
Expand Down Expand Up @@ -743,7 +730,6 @@ protected void LogQosEvent()

try
{
_metricHelper.SetPSHost(this.Host);
_metricHelper.LogQoSEvent(_qosEvent, IsUsageMetricEnabled, IsErrorMetricEnabled);
_metricHelper.FlushMetric();
WriteDebug("Finish sending metric.");
Expand Down Expand Up @@ -977,6 +963,9 @@ public virtual bool IsTerminatingError(Exception ex)
//Initialized once AzVersion is loaded.
//Format: AzurePowershell/Az0.0.0 %AZUREPS_HOST_ENVIROMENT%
public static string UserAgent { set; get; }
public static string PowerShellVersion { set; get; }
public static string PSHostVersion { set; get; }
public static string PSHostName { set; get; }

protected string LoadAzVersion()
{
Expand Down Expand Up @@ -1024,5 +1013,19 @@ protected string LoadAzVersion()
WriteDebug(string.Format("Sought all Az modules and got latest version {0}", ret));
return ret;
}

private string LoadPowerShellVersion()
{
try
{
var outputs = this.ExecuteScript<string>("$Host.Runspace.Version.ToString()");
return outputs[0];
}
catch (Exception e)
{
WriteDebug(string.Format("Cannot fetch PowerShell version due to exception: {0}", e.Message));
}
return DEFAULT_PSVERSION;
}
}
}
38 changes: 18 additions & 20 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,14 @@ public class MetricHelper

private AzurePSDataCollectionProfile _profile;

private static PSHost _host;

private static string _psVersion;

[Obsolete("Should use AzurePSCmdlet.PowerShellVersion")]
protected string PSVersion
{
get
{
if (_host != null)
{
_psVersion = _host.Version.ToString();
}
else
{
_psVersion = DefaultPSVersion;
}
return _psVersion;
return DefaultPSVersion;
}
}

public string HashMacAddress
{
get
Expand Down Expand Up @@ -256,9 +244,9 @@ private void LoadTelemetryClientContext(AzurePSQoSEvent qos, TelemetryContext cl
}
}

[Obsolete()]
public void SetPSHost(PSHost host)
{
_host = host;
}

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

eventProperties.Add("telemetry-version", "1");
// Breaking change of telemetry
// * 2, change host version to real PowerShell version. Original version was PowerShell host version which is not always the same as PS version
// and can be customized.
eventProperties.Add("telemetry-version", "2");
eventProperties.Add("Command", qos.CommandName);
eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
eventProperties.Add("ModuleName", qos.ModuleName);
eventProperties.Add("ModuleVersion", qos.ModuleVersion);
eventProperties.Add("HostVersion", qos.HostVersion);
eventProperties.Add("HostName", qos.PSHostName);
eventProperties.Add("OS", Environment.OSVersion.ToString());
eventProperties.Add("CommandParameters", qos.Parameters);
eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
eventProperties.Add("UserAgent", qos.UserAgent);
eventProperties.Add("HashMacAddress", HashMacAddress);
eventProperties.Add("PowerShellVersion", PSVersion);
eventProperties.Add("PowerShellVersion", qos.PSVersion);
eventProperties.Add("Version", qos.AzVersion);
eventProperties.Add("CommandParameterSetName", qos.ParameterSetName);
eventProperties.Add("CommandInvocationName", qos.InvocationName);
Expand Down Expand Up @@ -538,7 +530,12 @@ public class AzurePSQoSEvent
public string CommandName { get; set; }
public string ModuleName { get; set; }
public string ModuleVersion { get; set; }
//Version of PowerShell runspace ($Host.Runspace.Version)
public string PSVersion { get; set; }
//Host version of PowerShell ($Host.Version) which can be customized by PowerShell wrapper
public string HostVersion { get; set; }
//Host Name of PowerShell
public string PSHostName { get; set; }
public string AzVersion { get; set; }
public string UserAgent { get; set; }
public string Parameters { get; set; }
Expand Down Expand Up @@ -583,11 +580,12 @@ public void FinishQosEvent()

public override string ToString()
{
string ret = string.Format(
"AzureQoSEvent: CommandName - {0}; IsSuccess - {1}; Duration - {2}", CommandName, IsSuccess, Duration);
string ret = $"AzureQoSEvent: Module: {ModuleName}:{ModuleVersion}; CommandName: {CommandName}; PSVersion: {PSVersion}";
ret += $"; IsSuccess: {IsSuccess}; Duration: {Duration}";

if (Exception != null)
{
ret = $"{ret}; Exception - {Exception.Message};";
ret += $"; Exception: {Exception.Message};";
}
return ret;
}
Expand Down