Skip to content

Commit 1c6c787

Browse files
committed
Added PSVersion property to AppInsights
1 parent a62ba92 commit 1c6c787

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/ALC/ApplicationInsights.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ApplicationInsights
2626
/// <param name="initializationType">Information on what method has been used to establish a connection</param>
2727
/// <param name="assemblyVersion">The PnP PowerShell version in use</param>
2828
/// <param name="operatingSystem">The operating system on which PnP PowerShell is being used</param>
29-
public void Initialize(string serverLibraryVersion, string serverVersion, string initializationType, string assemblyVersion, string operatingSystem)
29+
public void Initialize(string serverLibraryVersion, string serverVersion, string initializationType, string assemblyVersion, string operatingSystem, string psVersion = "")
3030
{
3131
// Retrieve an instance of the telemetry client to use
3232
_telemetryClient = TelemetryClientFactory.GetTelemetryClient();
@@ -41,7 +41,8 @@ public void Initialize(string serverLibraryVersion, string serverVersion, string
4141
{ "ConnectionMethod", initializationType.ToString() }, // Information on what method has been used to establish a connection
4242
{ "Version", assemblyVersion }, // The PnP PowerShell version in use
4343
{ "Platform", "SPO" }, // Platform to which the connection has been made
44-
{ "OperatingSystem", operatingSystem} // The operating system on which PnP PowerShell is being used
44+
{ "OperatingSystem", operatingSystem}, // The operating system on which PnP PowerShell is being used
45+
{ "PSVersion", psVersion}
4546
};
4647
}
4748
}

src/Commands/Base/PnPConnection.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ internal static PnPConnection CreateWithACSAppOnly(Uri url, string realm, string
197197
{
198198
realm = GetRealmFromTargetUrl(url);
199199

200-
if(realm == null)
200+
if (realm == null)
201201
{
202202
throw new Exception($"Could not determine realm for the target site '{url}'. Please validate that a site exists at this URL.");
203203
}
@@ -355,7 +355,7 @@ internal static PnPConnection CreateWithManagedIdentity(Cmdlet cmdlet, string ur
355355
var defaultResource = $"{resourceUri.Scheme}://{resourceUri.Authority}";
356356
cmdlet.WriteVerbose("Acquiring token for resource " + defaultResource);
357357
var accessToken = TokenHandler.GetManagedIdentityTokenAsync(cmdlet, httpClient, defaultResource, userAssignedManagedIdentityObjectId).GetAwaiter().GetResult();
358-
358+
359359
using (var authManager = new PnP.Framework.AuthenticationManager(new System.Net.NetworkCredential("", accessToken).SecurePassword))
360360
{
361361
PnPClientContext context = null;
@@ -751,11 +751,40 @@ internal void InitializeTelemetry(ClientContext context, InitializationType init
751751
var coreAssembly = Assembly.GetExecutingAssembly();
752752
var operatingSystem = Utilities.OperatingSystem.GetOSString();
753753

754-
ApplicationInsights.Initialize(serverLibraryVersion, serverVersion, initializationType.ToString(), ((AssemblyFileVersionAttribute)coreAssembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version.ToString(), operatingSystem);
754+
ApplicationInsights.Initialize(serverLibraryVersion, serverVersion, initializationType.ToString(), ((AssemblyFileVersionAttribute)coreAssembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version.ToString(), operatingSystem, PSVersion);
755755
ApplicationInsights.TrackEvent("Connect-PnPOnline");
756756
}
757757
}
758758

759+
private static string PSVersion => (PSVersionLazy.Value);
760+
761+
private static readonly Lazy<string> PSVersionLazy = new Lazy<string>(
762+
() =>
763+
764+
{
765+
var caller = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(a => a.GetName().Name == "System.Management.Automation");
766+
//var caller = Assembly.GetCallingAssembly();
767+
var psVersionType = caller.GetType("System.Management.Automation.PSVersionInfo");
768+
if (null != psVersionType)
769+
{
770+
PropertyInfo propInfo = psVersionType.GetProperty("PSVersion");
771+
if (null == propInfo)
772+
{
773+
propInfo = psVersionType.GetProperty("PSVersion", BindingFlags.NonPublic | BindingFlags.Static);
774+
}
775+
var getter = propInfo.GetGetMethod(true);
776+
var version = getter.Invoke(null, new object[] { });
777+
778+
if (null != version)
779+
{
780+
var versionType = version.GetType();
781+
var versionProperty = versionType.GetProperty("Major");
782+
return ((int)versionProperty.GetValue(version)).ToString();
783+
}
784+
}
785+
return "";
786+
});
787+
759788
private static string PnPPSVersionTag => (PnPPSVersionTagLazy.Value);
760789

761790
private static readonly Lazy<string> PnPPSVersionTagLazy = new Lazy<string>(

src/Commands/Base/PnPPowerShellModuleInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace PnP.PowerShell.Commands.Base
1010
{
1111
public class PnPPowerShellModuleInitializer : IModuleAssemblyInitializer
1212
{
13-
private static string s_binBasePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),".."));
13+
private static string s_binBasePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ".."));
1414

1515
private static string s_binCommonPath = Path.Combine(s_binBasePath, "Common");
1616

0 commit comments

Comments
 (0)